problem with signal() and kill() functions

Hi everyone.

I'm new in c and now I'm studying how the process signals work. I was trying to create a program where the main process create N processes ( N=argc ) and when created, they send a signal to their father. Each time this signal is received a counter (oki++) is increased.

My output is a mess!!! Something is missing. I tought it would be the wait() but is not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int oki=0;



//OK
void ok(int sinal)
{
oki++;
printf("Ok vale %d\n",oki);
}


//MAIN
main(int argc, char **argv)
{
int pid[max],i,j,status, n, ppid;

n=atoi(argv[1]);
if (argc<=1)
	{
	printf("wrong sintax. \n\n");
	exit(1);
	}

signal(SIGUSR1, ok);

for(i=0;i<argc;i++)
	{
	pid[i]=fork();
		if(pid[i]==0)
		{
		ppid=getppid();
		printf("fathers pid  %d\n",ppid);
		kill(ppid, SIGUSR1);
			
		}	
		else
		printf("process created number %d\n", getpid());
		wait(&status);
		
	}


printf("done\n");
return(0);



}


This is my code.
When I run the program with "./teste 1 2 3 " I get on the screen:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

pubuntu@pubuntu:~$ ./teste 1 2 3
process created number 6438
fathers pid  6438
Oki is 1
process created number 6439
fathers pid  6439
process created number 6440
Oki is 1
fathers pid  6440
Oki is 1
process created number 6441
fathers pid  6441
done
Oki is 1
done
process created number 6440
fathers pid  6440
Oki is 2
done
done
process created number 6439
fathers pid  6439
Oki is 2
process created number 6444
fathers pid  6444
Oki is 2
done
done
process created number 6439
fathers pid  6439
Oki is 3
done
done
process created number 6438
fathers pid  6438
Oki is 2
process created number 6447
fathers pid  6447
process created number 6448
Oki is 2
fathers pid  6448
done
Oki is 2
done
process created number 6447
fathers pid  6447
Oki is 3
done
done
process created number 6438
fathers pid  6438
Oki is 3
process created number 6451
fathers pid  6451
done
Oki is 3
done
process created number 6438
fathers pid  6438
Oki is 4
done
done


That's far from what I was expecting. I was expecting something like:
"
process created number 6451
fathers pid 6450
oki is 1

process created number 6452
fathers pid 6450
oki is 2

.
.
.


process created number 6454
fathers pid 6450
oki is 4
done

What am I doing wrong ?
no one ?
I have no idea why it is kicking that value out. What are your includes?
If fork() returns 0 then you are in the child process, so the parent will be the creating process.

If fork() returns > 0 then you are still in the parent process. The returned value is the pid of the created child.

So you can see that you are printing the parent's pid twice.

Also bear in mind that the output to the console will not necessarily be in consecutive order. This is because you will have multiple processes running and they will be all be fighting to get their output to the console simultaneously.
Topic archived. No new replies allowed.