Skip to contentSkip to conversation

PROBLEM: fork() & setpriority()

DISCUSSION Rommer 2 participants 3 messages Jul 11, 2005

lore
Share & tools
lore

Rommer

author
#0 lore raw

Hello,

I have trouble with fork() and setpriority().
When priority of child process != priority of parent process and used SIGCHLD handler.
See example.

kernel 2.6.12.1, no SMP

Signature · 1 line
Best regards, Roman

#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

struct sigaction sa_chld;
int i = 0;

static void chld_handler (int signum) {
    int status = 0;
    wait (&status);
    printf ("child closed: status = %i\n", status >> 8);
}
void test (void) {
    pid_t pid = fork ();

    if (pid == 0) {
	setpriority (PRIO_PROCESS, 0, 0);
	printf ("helo from child, i = %i\n", i);
	exit (i);
    }
}
int main () {
    memset (&sa_chld, 0, sizeof (sa_chld));
    sa_chld.sa_handler = &chld_handler;
    sigaction (SIGCHLD, &sa_chld, NULL);

    setpriority (PRIO_PROCESS, 0, -1);
    while (1) {
	i++;
	test ();
	sleep (3);
    }
    return 0;
}
#1 lore raw
Quoted 6 lines · Rommer

Hello,

I have trouble with fork() and setpriority().
When priority of child process != priority of parent process and used SIGCHLD handler.
See example.

the example is buggy in that printf() isn't allowed in signal handlers btw...

author
#2 lore raw

Hello,

Quoted 4 lines · Arjan van de Ven
Quoted 2 lines · Rommer

Hello,

Quoted 9 lines
Quoted 4 lines

I have trouble with fork() and setpriority().
When priority of child process != priority of parent process and used SIGCHLD handler.
See example.

the example is buggy in that printf() isn't allowed in signal handlers btw...

ok, you can remove printf() from SIGCHLD handler.

Signature · 1 line
Best regards, Roman