Turns out that the optimal solution has been known since 1991. The answer is we need
$$\left\lceil\frac{7n}{6}\right\rceil$$
gloves to have $n$ doctors operating on $n$ patients without the risk of infecting each other. This number is optimal.
Ilan Vardi published it in chapter 10 of Computational Recreations in Mathematica (Addison-Wesley). Both the proof of optimality and the algorithm for achieving it are complex, but the main points are summarized below.
Partial Upper-bound
We prove the upper-bound for $n=2k$ and $m=3l$ ($m\geq n$) by giving an algorithm that works for $n$ doctors, $m$ patients using $\frac{n}{2}+\frac{2m}{3}+1$ gloves (which is equal to $\left\lceil\frac{7n}{6}\right\rceil$ for $n=m\neq 6k$ for some $k$).
As in the question, denote the patients as $p_i$, doctors as $d_i$, and gloves as $g_i$.
- $d_{2i-1}$ use $g_i$ for $i=1,\ldots,k$, $p_{3j-2}$ use $g'_{2j-1}$, and $p_{3j-1}$ use $g'_{2j}$ for $j=1,\ldots,l$. That's $k+2j$ gloves so far, and an extra glove $G$ will be used to protect $g'_i$.
- Have the operations using $d_{2i-1}-\overrightarrow{g_i}-\overrightarrow{g'_{2j-1}}-p_{3j-2}$ and $d_{2i-1}-\overrightarrow{g_i}-\overrightarrow{g'_{2j}}-p_{3j-1}$.
- Turn $g'_{2j}$ inside out to be used by $p_{3j}$.
- Have the operations using $d_{2i-1}-\overrightarrow{g_i}-\overrightarrow{G}-\overleftarrow{g'_{2j}}-p_{3j}$.
- Doctors $d_{2i-1}$ are now done. Turn their gloves $g_i$ inside out to be used by $d_{2i}$.
- Have the operations using $d_{2i}-\overleftarrow{g_i}-\overleftarrow{G}-\overrightarrow{g'_{2j-1}}-p_{3j-2}$ and $d_{2i}-\overleftarrow{g_i}-\overleftarrow{g'_{2j}}-p_{3j}$.
- Patients $p_{3j-2}$ are now done. Turn their gloves $g'_{2j-1}$ inside out to be used by $p_{3j-1}$.
- Have the operations using $d_{2i}-\overleftarrow{g_i}-\overleftarrow{g'_{2j-1}}-p_{3j-1}$.
- We are done.
Partial Lower-bound
- Make a graph with $n+m$ vertices, representing the doctors and patients. The gloves will be the edges.
- An edge will be formed between two vertices representing the gloves' sides infected by the corresponding doctor or patient. The direction of the edge depends on which side gets infected first. The side which gets infected first is the source of the edge, and the side which gets infected later is the target of the edge. If infection occurs simultaneously, take arbitrary direction.
For example, the 2 doctors 2 patient solution is the graph $d_1 \rightarrow p_1\ \ \ d_2 \rightarrow p_2$.
The partial upper-bound algorithm above has the graph in the form of $\overbrace{d\rightarrow d}^{\frac{n}{2}\text{ times}}\ \ \ \overbrace{p\rightarrow p \rightarrow p}^{\frac{2n}{3}\text{ times}}$.
Now, the most efficient use of a glove is to use one side for one person, and the other side for another person, for all operations. This gives one glove for two persons. We want to maximize this use as much as possible. The second type, which is the next most efficient use is for three persons to use two gloves.
Note that for the 2-person setup to work, one person must finish all operations first with that glove, and only then give it to the second person. This implies that 2-person setup cannot occur on both the doctors side and the patients side. Another observation is that there can be at most one 2-person setup for doctor-patient and one 2-person setup for patient-doctor.
By these observations and the fact that no vertex can be isolated, the optimal solution must be in the form of $d \rightarrow p\ \ \ d\leftarrow p\ \ \ \overbrace{d\rightarrow d}^{\frac{n-2}{2}\text{ times}}\ \ \ \overbrace{p\rightarrow \ldots \rightarrow p}^{\text{rest of the patients}}$, with the "rest of the patients" part cannot contain 2-person setup.
This means that at least there has to be $\frac{n-2}{2}+2+\frac{2}{3}(m-2) = \frac{n}{2}+\frac{2m}{3}-\frac{1}{3}$ gloves. Note that this gives $7n$ gloves for $n$ multiples of 6, whereas the partial upper-bound above gives $7n+1$.
Rest of the proof
The rest of the proof from the book generalizes the above upper-bound and lower-bound for arbitrary $n,m$ (and not just $n=2k, m=3l$), also resolves the discrepancy between the upper-bound and the lower-bound for $n,m$ multiples of 6, finally concluding that $\displaystyle\left\lceil\frac{n}{2}+\frac{2m}{3}\right\rceil$ is the optimal number of gloves required for $n$ doctors and $m$ patients.
For this question, since $n=m$, we have the optimal number of gloves is $\displaystyle\left\lceil\frac{7n}{6}\right\rceil$.