2

I created processes. One of them prints "1" 30 times. Another prints "2" 30 times.I want these processes to print their numbers in turn.

Like this:

1
2
1
2
...

But every time I run the program, the numbers are displayed differently:

1,2,2,1; 1,2,1,2,2,1,1,2.

Can someone tell me,please,how to solve this problem?

Dean
  • 65
  • 5
  • 1
    Once a process is forked, they can be controlled by a shared variable. A value of `0` would mean process `A` will print, and value of `1` will mean process `B` will print. They can toggle the value after printing once. – Haris Oct 18 '15 at 08:37
  • No its not about pid that's an identifier used by the kernel to simplify its jobs. I thinks he is talking about a variable which is meant to control when the second process will do its job which implies to work with mmap, that is shared memory between processes. – user5159806 Oct 18 '15 at 10:09
  • Not all programs run at the same speed. You cannot control the speed of a program after `fork()`. On uniprocessor systems neither of two can get the cpu at the same time, but this doesn't mean they get the same time executing. – Luis Colorado Oct 20 '15 at 10:46

3 Answers3

3

This is an expected behaviour. After the process is forked (which is a quite small duration), two processes will print 2 or 1 depending on their id's. Because sleep method resolution is 1 second and time difference between two forked processes is too small compared to this, the order of printing id's will not be definite because the two processes will call printf function is quite close times (on the micro-second resolution maybe). To be able to order printing numbers, you can add a delay to one of the processes:

if(pid==0)
    {
        for(i=1;i<=30;i++)
        {
            printf("%s\n","2");
            sleep(2);
        }
    }
    else
    {
        sleep(1);
        for(i=1;i<30;i++)
        {
            printf("%s\n","1");
            sleep(2);
        }
    }

As a result, forked processes are not expected to run in a perfect synchrony. In order to achive interprocess synchrony you can use inter process level shared variables: https://stackoverflow.com/a/13274800/2183287

Community
  • 1
  • 1
fatihk
  • 7,789
  • 1
  • 26
  • 48
  • @Dean edited the answer, the problem is sleep() resolution of 1s which is quite large to differentiate between two processes wihch have almost the same starting time – fatihk Oct 18 '15 at 08:54
  • 4
    The solution of yours is not robust. It may work for some cases but it definitely will not work for many cases. Please read about **race conditions**. – Haris Oct 18 '15 at 08:56
  • 1
    @Haris, I am not offering a solution but explaining the source of the problem – fatihk Oct 18 '15 at 08:59
  • 1
    Your answers says *To overcome this issue,..*. That means thats a solution. – Haris Oct 18 '15 at 09:00
  • @Haris, and you did use ellipsis after a comma, what does it mean? Words are important, but this is not a witch hunt. – Luis Colorado Oct 20 '15 at 10:49
2

Processes run entirely independent of each other. They may run simultaneously, they may run one after the other, they may run one at a time with a switch between them at non-deterministic times, or any mixture of these "modes". There is simply no telling which process runs when.

As such, the only way to make processes cooperate safely is to make them communicate. And here you have a lot of options:

  • Pipes allow you to send unidirectional messages between two forked processes. This communication is blocking by default.

  • Locks allow one process exclusive access to whatever resource you like.

  • Shared memory regions allow processes to send each other data without the need to wait for the other process.

  • Files can be used instead of locks or shared memory regions. They are much more heavy-weight, but don't require the processes to be of common decent.

Whatever communication you use, you must use one to get correctly coordinated behavior.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
1

After seen these comments, I feel that I need to work out my own answer.

Since two or n processes are independent of each other, they don't know about the existence of other processes and so we need some way to make them communicate without breaking the system.

This is known as IPC or Inter Process Communication and one way to accomplish this is shared memory. That is a region of memory which can be shared between two or more processes.

But how we access that shared memory, with pointers and we could do something like this:

The father process will allocate a chunk of shared memory with mmap. Now this region can be accessed with pointers by processes to talk between them and share data.