[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Backround process, fork & vfork, synchronization methods?



I don't think his is anything strange, you have two processes writing to
the same file (stdout) and they are not synchronised.

/Johan

----- Original Message -----
From: <Timo.Sakari@xxxxxxx.fi>
To: <dev-etrax@xxxxxxx.com>
Sent: Monday, March 19, 2001 6:53 PM
Subject: RE: Backround process, fork & vfork, synchronization methods?


> Replying to my own mail:
>
> > Question: Does Axis/Cris provide standard Unix synchronization
> > methods like semaphores or mutexes? What kind of interprocess
> > communication methods (pipes, message queues etc.) are supported?
>
> They seem to exist. I found a simple pipe test app from
> http://www.linuxdoc.org/LDP/lpg/node11.html#SECTION00722000000000000000,
> modified it a little and ran it on Axis.
>
> #include <stdio.h>
> #include <unistd.h>
> #include <sys/types.h>
>
> int main(void)
> {
>         int     fd[2], nbytes;
>         int     counter = 5;
>         int     tmp = 1;
>         int     msgs_received = 0;
>         pid_t   childpid;
>         /* char    string[] = "Hello, world!\n"; */
>         char    string[80];
>         char    readbuffer[80];
>
>         pipe(fd);
>
>         if((childpid = fork()) == -1)
>         {
>                 perror("fork");
>                 exit(1);
>         }
>
>         if(childpid == 0)
>         {
>                 counter = 1000;
>                 printf("\nchild: running...");
>                 /* Child process closes up input side of pipe */
>                 close(fd[0]);
>
>                 while (counter--)
>                     {
>                     printf("\nchild: counter: %d", counter);
>
>                     /* Send "string" through the output side of pipe */
>                     sprintf(string, "message from child, part %d\n",
tmp++);
>                     write(fd[1], string, strlen(string));
>                     }
>
>                 printf("\nchild: exiting...");
>                 exit(0);
>         }
>         else
>         {
>                 printf("\nparent: running...");
>                 /* Parent process closes up output side of pipe */
>                 close(fd[1]);
>
>                 while (counter--)
>                     {
>                     printf("\nparent: counter: %d", counter);
>                     /* Read in a string from the pipe */
>                     nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
>                     msgs_received++;
>                     printf("\nParent: received string num %d: %s",
>                         msgs_received, readbuffer);
>                     }
>         }
>
>         printf("\nParent returning...");
>         return(0);
> }
>
>
> The output is quite interesting:
>
> parent: running...
> parent: counter: 4
> child: running...
> child: counter: 999
> child: counter: 998
> child: counter: 997
> child: counter: 996
> child: counter: 995
> child: counter: 994
> child: ld:
> Parent: received string num 1: message from child, part 1
> message from child, part 2
> message from child, part 3
> parent: counter: 3
> Parent: received string num 2:
> counter: 993
> child: counter: 992
> child: counter: 991
> child: counter: 990
> child: counter: 989
> child: counter: 988
> child: counter: 987
> child: counter: 986
> child: counter: 985
> child: counter: 984
> child: counter: 983
> child: counter: 982
> child: counter: 981
> child: counter: 980
> child: counter: 979
> child: counter: 978
> child: counter: 977
> child: counter: 976
> child: counter: 975
> child: counter: 974
> child: counter: 973
> child: counter: 972
> child: counter: 971
> child: counter: 970
> child: counter: 969
> child: counter: 968
> child: counter: 967
> child: counter: 966
> child: counter: 965
> child: counter: 964
> child: counter: 963
> child: counter: 962
> child: counter: 961
> child: counter: 960
> child: counter: 959
> child: counter: 958
> child: counter: 957
> child: counter: 956
> child: counter: 955
> child: counter: 954
> child: counter: 953
> child: counter: 952
> child: counter: 951
> child: counter: 950
> child: counter: 949
> child: counter: 948
> child: counter: 947
> child: counter: 946
> child: counter: 945
> child: counter: 944
> child: counter: 943
> child: counter: 942
> child: counter: 941
> child: counter: 940
> child: counter: 939
> child: counter: 938
> child: counter: 937
> child: counter: 936
> child: counter: 935
> child: counter: 934
> child: counter: 933
> child: counter: 932
> child: counter: 931
> child: counter: 930
> chimessage from child, part 4
> message from child, part 5
> message from child, part
> parent: counter: 2
> Parent: received string num 3: 6
> message from child, part 7
> message from child, part 8
> message from child, part
> parent: counter: 1
> Parent: received string num 4:  9
> message from child, part 10
> message from child, part 11
> message from child, p
> parent: counter: 0
> Parent: received string num 5: art 12
> message from child, part 13
> message from child, part 14
> message from chil
> Parent returning...ld: counter:
>
> The printf output seems quite messy. I guess your implementation
> of printf (and other libraries?) in not "multiprocess safe"?
>
> Timo
>