patch-1.3.33 linux/kernel/fork.c

Next file: linux/kernel/ksyms.c
Previous file: linux/init/main.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v1.3.32/linux/kernel/fork.c linux/kernel/fork.c
@@ -29,35 +29,27 @@
 
 int nr_tasks=1;
 int nr_running=1;
-long last_pid=0;
 
 static int find_empty_process(void)
 {
 	int i;
-	int this_user_tasks;
 	struct task_struct *p;
 
 	if (nr_tasks >= NR_TASKS - MIN_TASKS_LEFT_FOR_ROOT) {
 		if (current->uid)
 			return -EAGAIN;
 	}
-repeat:
-	if(smp_threads_ready) {
-		if ((++last_pid) & 0xffff8000)
-			last_pid=1;
-	}
-	this_user_tasks = 0;
-	for_each_task (p) {
-		if (p->uid == current->uid)
-			this_user_tasks++;
-		if (smp_threads_ready && (p->pid == last_pid ||
-		    p->pgrp == last_pid ||
-		    p->session == last_pid))
-			goto repeat;
+	if (current->uid) {
+		long max_tasks = current->rlim[RLIMIT_NPROC].rlim_cur;
+
+		if (max_tasks < nr_tasks) {
+			for_each_task (p) {
+				if (p->uid == current->uid)
+					if (--max_tasks < 0)
+						return -EAGAIN;
+			}
+		}
 	}
-	if (this_user_tasks > current->rlim[RLIMIT_NPROC].rlim_cur)
-		if (current->uid)
-			return -EAGAIN;
 	for (i = 0 ; i < NR_TASKS ; i++) {
 		if (!task[i])
 			return i;
@@ -65,6 +57,25 @@
 	return -EAGAIN;
 }
 
+static int get_pid(unsigned long flags)
+{
+	static int last_pid = 0;
+	struct task_struct *p;
+
+	if (flags & CLONE_PID)
+		return current->pid;
+repeat:
+	if ((++last_pid) & 0xffff8000)
+		last_pid=1;
+	for_each_task (p) {
+		if (p->pid == last_pid ||
+		    p->pgrp == last_pid ||
+		    p->session == last_pid)
+			goto repeat;
+	}
+	return last_pid;
+}
+
 static int dup_mmap(struct mm_struct * mm)
 {
 	struct vm_area_struct * mpnt, **p, *tmp;
@@ -214,7 +225,7 @@
 	*(unsigned long *) p->kernel_stack_page = STACK_MAGIC;
 	p->state = TASK_UNINTERRUPTIBLE;
 	p->flags &= ~(PF_PTRACED|PF_TRACESYS);
-	p->pid = last_pid;
+	p->pid = get_pid(clone_flags);
 	p->next_run = NULL;
 	p->prev_run = NULL;
 	p->p_pptr = p->p_opptr = current;

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen, slshen@lbl.gov with Sam's (original) version
of this