simple process synchronization

How to make a program which fork() child process then do simple process synchronization to guarantee that the main process has higher PID than child process. So i`m stuck at this code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

void main()
{
int main_id, child_id;

child_id = fork(); 
main_id = getpid(); 

if(child_id > main_id){
		
    }
}


Please help a newbie like me.
It isn't up to you to decide what any process's PID will be.

The system gives each process a number, which it is stuck with so long as it is not dead (that is, so long as it has an entry in the system process table).

Typically child process numbers will be higher than parent process numbers, unless the next available PID would be too large, in which case the PID is wrapped back down to the lowest available PID.


What exactly are you trying to do with this?
It is just a problem which an annoying teacher in the uni gave us two exams in a row. He lecture on UNIX but gave such programming problems that we couldnt resolve because we have learn only C++ not UNIX related.
Line 10 will nearly always evaluate to true unless as Duoas says, the PIDs have wrapped around.
To make the parent a higher PID fork again, making the new child the parent.
I figured it must be something like that. If the exercise really is to try to control the child's PID, raise your hand in the middle of class and say something like, "Could you tell us how to modify a child's PID?" [edit] BTW, you had better be pretty sure he wants you to do that before giving him a hard time.. otherwise you will look silly instead of him.[/edit]

If all he wants is for you to check then you seem to have it pretty good start.

The only trick is to be careful to follow the fork() pattern on line 7 so that you know whether you are parent or child on lines 8+.
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
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
  {
  pid_t pid = fork();

  switch (pid)
    {
    case -1:
      fprintf( stderr, "Failure to fork(). Program aborted.\n" );
      return 1;

    case 0:
      printf( "I am the child. My PID is %u.\n", getpid() );
      break;

    default:
      printf( "I am the parent. My PID is %u. My child's PID is %u.\n", getpid(), pid );
      waitpid( pid, NULL, 0 );  // wait for child to terminate
    }

  return 0;
  }
Be sure to take a look through the documentation for stuff like this: http://linux.die.net/man/2/fork
The pipe page (follow the links) gives a full-blown example of how to do some IPC between parent and child.

Hope this helps.
Last edited on
Thanks Duoas, this really shed light into the problem. Unless it is utterly needed I won't raise any questions toward him :D

That default switch really helped me a lot. Yesterday I had hard time googling about unix programing and the die.net is really great source to learn about it. Thank you again.
Google is a cranky friend.

For unix stuff, you can usually google for the man page. For example, I found that link for you with "man 2 fork". (That is, list the man page in section two for the fork function.) Even if you don't know exactly what you are looking for, wandering around the general pages of interest will often find you something more useful.

I find that all of the following are useful resources:
http://www.linuxmanpages.com/
http://man.he.net/
http://linux.die.net/man/

Enjoy!
Topic archived. No new replies allowed.