to use the semaphores, you have to allocate them using shared memory.
as once you call fork(), both the process will have their own data, so
the semaphores allocated in parent will differ from child. you can
have a shared memory (man shmget) to allocate semaphores and use it..

btw, i think we are missing the essence of the question here. the code
needs a "condition" so that both if and else gets executed. the
printf("hello") and printf("world") are just instructions in both the
code. the order doesn't matter when you have both the code executed.

On Fri, Jun 3, 2011 at 11:55 PM, nitish goyal <[email protected]> wrote:
> But if i have to make sure that hello should be printed first, then i have
> to apply synchronization.
> please answer my question. how we can semaphores over here
>
> On Fri, Jun 3, 2011 at 11:26 PM, nicks <[email protected]> wrote:
>>
>> @naveen.. thanks...i got it finally :)
>>
>> On Fri, Jun 3, 2011 at 10:35 AM, Naveen Kumar <[email protected]>
>> wrote:
>>>
>>> when we fork a new process, parent gets the PID of the chid as return
>>> value and child will get 0 as return value.
>>> Same address space is copied and both of them start executing this
>>> program in their own address space.
>>> In Modern OSes 99% of the time child comes first so child process is
>>> execute else part and parent will execute if place.
>>>
>>>
>>>
>>> On Fri, Jun 3, 2011 at 10:58 PM, nicks <[email protected]>
>>> wrote:
>>>>
>>>> i mean why both the if else statements are working by using fork ?
>>>>
>>>> On Fri, Jun 3, 2011 at 10:20 AM, Naveen Kumar
>>>> <[email protected]> wrote:
>>>>>
>>>>> Process don't share address space when forked.
>>>>>
>>>>>
>>>>> On Fri, Jun 3, 2011 at 10:40 PM, nitish goyal <[email protected]>
>>>>> wrote:
>>>>>>
>>>>>> @ Lalit
>>>>>> You are right.
>>>>>>
>>>>>> that's why i am saying how i can use semaphores in the above example
>>>>>>
>>>>>> On Fri, Jun 3, 2011 at 10:37 PM, LALIT SHARMA <[email protected]>
>>>>>> wrote:
>>>>>>>
>>>>>>> While using fork(), child shares parent address space ,
>>>>>>>
>>>>>>> Correct me If I am wrong ..
>>>>>>>
>>>>>>> On Fri, Jun 3, 2011 at 10:28 PM, nitish goyal
>>>>>>> <[email protected]> wrote:
>>>>>>> > Hi all,
>>>>>>> >
>>>>>>> > I am stuck with this code..Can anyone tell me how to implement
>>>>>>> > semaphores in
>>>>>>> > fork system call
>>>>>>> > Code:
>>>>>>> >
>>>>>>> > #include<stdio.h>
>>>>>>> >
>>>>>>> > int signal(int *n);
>>>>>>> > int wait(int *n);
>>>>>>> >
>>>>>>> >
>>>>>>> > int main()
>>>>>>> > {
>>>>>>> >     int n;
>>>>>>> >     n=0;
>>>>>>> >     if(fork())
>>>>>>> >     {
>>>>>>> >         printf("Hello");
>>>>>>> >         signal(&n);
>>>>>>> >     }
>>>>>>> >     else
>>>>>>> >     {
>>>>>>> >         wait(&n);
>>>>>>> >         printf("World");
>>>>>>> >     }
>>>>>>> > }
>>>>>>> > int signal(int *n)
>>>>>>> > {
>>>>>>> >     (*n)++;
>>>>>>> > }
>>>>>>> > int wait(int *n)
>>>>>>> > {
>>>>>>> >     while((*n)<=0);
>>>>>>> >     (*n)--;
>>>>>>> > }
>>>>>>> >
>>>>>>> > Since parent process and child process will be having different
>>>>>>> > address
>>>>>>> > spaces...so change of n in one address space will not be visible to
>>>>>>> > other.so
>>>>>>> > please tell me how to make n visible to both the processes
>>>>>>> >
>>>>>>> > On Fri, Jun 3, 2011 at 2:45 PM, Subhransu
>>>>>>> > <[email protected]>
>>>>>>> > wrote:
>>>>>>> >>
>>>>>>> >> Here you go in C code
>>>>>>> >>  http://codepad.org/gk6AZj0T
>>>>>>> >>
>>>>>>> >>
>>>>>>> >> int main()
>>>>>>> >> {
>>>>>>> >> if(printf("hello")!=0) {
>>>>>>> >>      printf("world");
>>>>>>> >> }
>>>>>>> >> else {
>>>>>>> >>      printf("SCREWED ! ! !"); }
>>>>>>> >>
>>>>>>> >> return 0;
>>>>>>> >> }
>>>>>>> >>
>>>>>>> >>
>>>>>>> >> Subhransu Panigrahi
>>>>>>> >>
>>>>>>> >> Mobile: +91-9840931538
>>>>>>> >> Email: [email protected]
>>>>>>> >>
>>>>>>> >>
>>>>>>> >> On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar
>>>>>>> >> <[email protected]>
>>>>>>> >> wrote:
>>>>>>> >>>
>>>>>>> >>> Hi Shachindra,
>>>>>>> >>> I don't think letters will be jumbled because we a calling one
>>>>>>> >>> api to
>>>>>>> >>> output on console & tty's driver takes whole line and output it
>>>>>>> >>> at once.
>>>>>>> >>>
>>>>>>> >>> On Fri, Jun 3, 2011 at 12:40 PM, Vishal Thanki
>>>>>>> >>> <[email protected]>
>>>>>>> >>> wrote:
>>>>>>> >>>>
>>>>>>> >>>> @sachindra, @naveen,
>>>>>>> >>>> this was just a plain trick to execute "if" and "else" block. i
>>>>>>> >>>> agree
>>>>>>> >>>> with your concerns :)
>>>>>>> >>>>
>>>>>>> >>>> 2011/6/3 Vιиodh <[email protected]>:
>>>>>>> >>>> > @vishal:
>>>>>>> >>>> > can u explain the fork()  solution??
>>>>>>> >>>> >
>>>>>>> >>>> > On Fri, Jun 3, 2011 at 12:16 PM, Shachindra A C
>>>>>>> >>>> > <[email protected]>
>>>>>>> >>>> > wrote:
>>>>>>> >>>> >>
>>>>>>> >>>> >> There can be some synchronisation problems with fork() right?
>>>>>>> >>>> >> say
>>>>>>> >>>> >> world
>>>>>>> >>>> >> might get printed first...or maybe the letters can get
>>>>>>> >>>> >> jumbled
>>>>>>> >>>> >> too...We
>>>>>>> >>>> >> cannot guarantee the order of execution unless we use
>>>>>>> >>>> >> semaphores.
>>>>>>> >>>> >>
>>>>>>> >>>> >> On Fri, Jun 3, 2011 at 12:14 PM, Naveen Kumar
>>>>>>> >>>> >> <[email protected]>
>>>>>>> >>>> >> wrote:
>>>>>>> >>>> >>>
>>>>>>> >>>> >>> oh yes,
>>>>>>> >>>> >>> gud one
>>>>>>> >>>> >>>
>>>>>>> >>>> >>> On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
>>>>>>> >>>> >>> <[email protected]>
>>>>>>> >>>> >>> wrote:
>>>>>>> >>>> >>>>
>>>>>>> >>>> >>>> vishal@ubuntu:~/progs/c\ 12:11:53 PM >$ cat fork.c
>>>>>>> >>>> >>>> #include <stdio.h>
>>>>>>> >>>> >>>> #include <stdlib.h>
>>>>>>> >>>> >>>>
>>>>>>> >>>> >>>> int main()
>>>>>>> >>>> >>>> {
>>>>>>> >>>> >>>>        if (fork()) {
>>>>>>> >>>> >>>>                printf("hello ");
>>>>>>> >>>> >>>>        } else {
>>>>>>> >>>> >>>>                printf("world\n");
>>>>>>> >>>> >>>>        }
>>>>>>> >>>> >>>>        return 0;
>>>>>>> >>>> >>>> }
>>>>>>> >>>> >>>> vishal@ubuntu:~/progs/c\ 12:11:56 PM >$ gcc fork.c
>>>>>>> >>>> >>>> vishal@ubuntu:~/progs/c\ 12:12:06 PM >$ ./a.out
>>>>>>> >>>> >>>> hello world
>>>>>>> >>>> >>>>
>>>>>>> >>>> >>>>
>>>>>>> >>>> >>>> On Fri, Jun 3, 2011 at 12:09 PM, Naveen Kumar
>>>>>>> >>>> >>>> <[email protected]> wrote:
>>>>>>> >>>> >>>> > Hi Vishal,
>>>>>>> >>>> >>>> >
>>>>>>> >>>> >>>> > Can you show us how it be done with fork?
>>>>>>> >>>> >>>> >
>>>>>>> >>>> >>>> > On Fri, Jun 3, 2011 at 12:02 PM, Vishal Thanki
>>>>>>> >>>> >>>> > <[email protected]>
>>>>>>> >>>> >>>> > wrote:
>>>>>>> >>>> >>>> >>
>>>>>>> >>>> >>>> >> can use fork() also..
>>>>>>> >>>> >>>> >>
>>>>>>> >>>> >>>> >> On Fri, Jun 3, 2011 at 11:57 AM, anand karthik
>>>>>>> >>>> >>>> >> <[email protected]> wrote:
>>>>>>> >>>> >>>> >> > (!printf("Hello"))
>>>>>>> >>>> >>>> >> >
>>>>>>> >>>> >>>> >> > On Jun 3, 2011 11:52 AM, "Arpit Mittal"
>>>>>>> >>>> >>>> >> > <[email protected]>
>>>>>>> >>>> >>>> >> > wrote:
>>>>>>> >>>> >>>> >> >> Please help me in this question.
>>>>>>> >>>> >>>> >> >>
>>>>>>> >>>> >>>> >> >> What's the "condition" so that the following code
>>>>>>> >>>> >>>> >> >> prints both
>>>>>>> >>>> >>>> >> >> HelloWorld !
>>>>>>> >>>> >>>> >> >>
>>>>>>> >>>> >>>> >> >> if "condition"
>>>>>>> >>>> >>>> >> >> printf ("Hello");
>>>>>>> >>>> >>>> >> >> else
>>>>>>> >>>> >>>> >> >> printf("World");
>>>>>>> >>>> >>>> >> >>
>>>>>>> >>>> >>>> >> >>
>>>>>>> >>>> >>>> >> >>
>>>>>>> >>>> >>>> >> >>
>>>>>>> >>>> >>>> >> >> --
>>>>>>> >>>> >>>> >> >> -Arpit Mittal
>>>>>>> >>>> >>>> >> >> 6th Semester,
>>>>>>> >>>> >>>> >> >> Indian Institute of Information Technology,Allahabad
>>>>>>> >>>> >>>> >> >> Email : [email protected]
>>>>>>> >>>> >>>> >> >> [email protected]
>>>>>>> >>>> >>>> >> >> Contact : +91-8853049787
>>>>>>> >>>> >>>> >> >>
>>>>>>> >>>> >>>> >> >> Let every man be respected as an individual and no
>>>>>>> >>>> >>>> >> >> man
>>>>>>> >>>> >>>> >> >> idolized.
>>>>>>> >>>> >>>> >> >>
>>>>>>> >>>> >>>> >> >> --
>>>>>>> >>>> >>>> >> >> You received this message because you are subscribed
>>>>>>> >>>> >>>> >> >> to the
>>>>>>> >>>> >>>> >> >> Google
>>>>>>> >>>> >>>> >> >> Groups
>>>>>>> >>>> >>>> >> >> "Algorithm Geeks" group.
>>>>>>> >>>> >>>> >> >> To post to this group, send email to
>>>>>>> >>>> >>>> >> >> [email protected].
>>>>>>> >>>> >>>> >> >> To unsubscribe from this group, send email to
>>>>>>> >>>> >>>> >> >> [email protected].
>>>>>>> >>>> >>>> >> >> For more options, visit this group at
>>>>>>> >>>> >>>> >> >> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>>> >>>> >> >>
>>>>>>> >>>> >>>> >> >
>>>>>>> >>>> >>>> >> > --
>>>>>>> >>>> >>>> >> > You received this message because you are subscribed
>>>>>>> >>>> >>>> >> > to the
>>>>>>> >>>> >>>> >> > Google
>>>>>>> >>>> >>>> >> > Groups
>>>>>>> >>>> >>>> >> > "Algorithm Geeks" group.
>>>>>>> >>>> >>>> >> > To post to this group, send email to
>>>>>>> >>>> >>>> >> > [email protected].
>>>>>>> >>>> >>>> >> > To unsubscribe from this group, send email to
>>>>>>> >>>> >>>> >> > [email protected].
>>>>>>> >>>> >>>> >> > For more options, visit this group at
>>>>>>> >>>> >>>> >> > http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>>> >>>> >> >
>>>>>>> >>>> >>>> >>
>>>>>>> >>>> >>>> >> --
>>>>>>> >>>> >>>> >> You received this message because you are subscribed to
>>>>>>> >>>> >>>> >> the
>>>>>>> >>>> >>>> >> Google
>>>>>>> >>>> >>>> >> Groups
>>>>>>> >>>> >>>> >> "Algorithm Geeks" group.
>>>>>>> >>>> >>>> >> To post to this group, send email to
>>>>>>> >>>> >>>> >> [email protected].
>>>>>>> >>>> >>>> >> To unsubscribe from this group, send email to
>>>>>>> >>>> >>>> >> [email protected].
>>>>>>> >>>> >>>> >> For more options, visit this group at
>>>>>>> >>>> >>>> >> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>>> >>>> >>
>>>>>>> >>>> >>>> >
>>>>>>> >>>> >>>> >
>>>>>>> >>>> >>>> >
>>>>>>> >>>> >>>> > --
>>>>>>> >>>> >>>> > Cheers
>>>>>>> >>>> >>>> > Naveen Kumar
>>>>>>> >>>> >>>> >
>>>>>>> >>>> >>>> > --
>>>>>>> >>>> >>>> > You received this message because you are subscribed to
>>>>>>> >>>> >>>> > the
>>>>>>> >>>> >>>> > Google
>>>>>>> >>>> >>>> > Groups
>>>>>>> >>>> >>>> > "Algorithm Geeks" group.
>>>>>>> >>>> >>>> > To post to this group, send email to
>>>>>>> >>>> >>>> > [email protected].
>>>>>>> >>>> >>>> > To unsubscribe from this group, send email to
>>>>>>> >>>> >>>> > [email protected].
>>>>>>> >>>> >>>> > For more options, visit this group at
>>>>>>> >>>> >>>> > http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>>> >>>> >
>>>>>>> >>>> >>>>
>>>>>>> >>>> >>>> --
>>>>>>> >>>> >>>> You received this message because you are subscribed to the
>>>>>>> >>>> >>>> Google
>>>>>>> >>>> >>>> Groups "Algorithm Geeks" group.
>>>>>>> >>>> >>>> To post to this group, send email to
>>>>>>> >>>> >>>> [email protected].
>>>>>>> >>>> >>>> To unsubscribe from this group, send email to
>>>>>>> >>>> >>>> [email protected].
>>>>>>> >>>> >>>> For more options, visit this group at
>>>>>>> >>>> >>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>>> >>>>
>>>>>>> >>>> >>>
>>>>>>> >>>> >>>
>>>>>>> >>>> >>>
>>>>>>> >>>> >>> --
>>>>>>> >>>> >>> Cheers
>>>>>>> >>>> >>> Naveen Kumar
>>>>>>> >>>> >>>
>>>>>>> >>>> >>> --
>>>>>>> >>>> >>> You received this message because you are subscribed to the
>>>>>>> >>>> >>> Google
>>>>>>> >>>> >>> Groups
>>>>>>> >>>> >>> "Algorithm Geeks" group.
>>>>>>> >>>> >>> To post to this group, send email to
>>>>>>> >>>> >>> [email protected].
>>>>>>> >>>> >>> To unsubscribe from this group, send email to
>>>>>>> >>>> >>> [email protected].
>>>>>>> >>>> >>> For more options, visit this group at
>>>>>>> >>>> >>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>>> >>
>>>>>>> >>>> >>
>>>>>>> >>>> >>
>>>>>>> >>>> >> --
>>>>>>> >>>> >> Regards,
>>>>>>> >>>> >> Shachindra A C
>>>>>>> >>>> >>
>>>>>>> >>>> >> --
>>>>>>> >>>> >> You received this message because you are subscribed to the
>>>>>>> >>>> >> Google
>>>>>>> >>>> >> Groups
>>>>>>> >>>> >> "Algorithm Geeks" group.
>>>>>>> >>>> >> To post to this group, send email to
>>>>>>> >>>> >> [email protected].
>>>>>>> >>>> >> To unsubscribe from this group, send email to
>>>>>>> >>>> >> [email protected].
>>>>>>> >>>> >> For more options, visit this group at
>>>>>>> >>>> >> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>>> >
>>>>>>> >>>> >
>>>>>>> >>>> >
>>>>>>> >>>> > --
>>>>>>> >>>> > With regards,
>>>>>>> >>>> > Vιиodh
>>>>>>> >>>> >
>>>>>>> >>>> > --
>>>>>>> >>>> > You received this message because you are subscribed to the
>>>>>>> >>>> > Google
>>>>>>> >>>> > Groups
>>>>>>> >>>> > "Algorithm Geeks" group.
>>>>>>> >>>> > To post to this group, send email to
>>>>>>> >>>> > [email protected].
>>>>>>> >>>> > To unsubscribe from this group, send email to
>>>>>>> >>>> > [email protected].
>>>>>>> >>>> > For more options, visit this group at
>>>>>>> >>>> > http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>>> >
>>>>>>> >>>>
>>>>>>> >>>> --
>>>>>>> >>>> You received this message because you are subscribed to the
>>>>>>> >>>> Google
>>>>>>> >>>> Groups "Algorithm Geeks" group.
>>>>>>> >>>> To post to this group, send email to [email protected].
>>>>>>> >>>> To unsubscribe from this group, send email to
>>>>>>> >>>> [email protected].
>>>>>>> >>>> For more options, visit this group at
>>>>>>> >>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>>>
>>>>>>> >>>
>>>>>>> >>>
>>>>>>> >>>
>>>>>>> >>> --
>>>>>>> >>> Cheers
>>>>>>> >>> Naveen Kumar
>>>>>>> >>>
>>>>>>> >>> --
>>>>>>> >>> You received this message because you are subscribed to the
>>>>>>> >>> Google Groups
>>>>>>> >>> "Algorithm Geeks" group.
>>>>>>> >>> To post to this group, send email to [email protected].
>>>>>>> >>> To unsubscribe from this group, send email to
>>>>>>> >>> [email protected].
>>>>>>> >>> For more options, visit this group at
>>>>>>> >>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >>
>>>>>>> >> --
>>>>>>> >> You received this message because you are subscribed to the Google
>>>>>>> >> Groups
>>>>>>> >> "Algorithm Geeks" group.
>>>>>>> >> To post to this group, send email to [email protected].
>>>>>>> >> To unsubscribe from this group, send email to
>>>>>>> >> [email protected].
>>>>>>> >> For more options, visit this group at
>>>>>>> >> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >
>>>>>>> >
>>>>>>> >
>>>>>>> > --
>>>>>>> > Regards,
>>>>>>> > Nitish Goyal
>>>>>>> > Undergraduate Student Of NSIT,
>>>>>>> > Computer Engineering (B.E.)
>>>>>>> > Asst. PlaceComm 2011
>>>>>>> > contact me:- +91-9999605824
>>>>>>> > Reply at :-  [email protected]
>>>>>>> >
>>>>>>> > --
>>>>>>> > You received this message because you are subscribed to the Google
>>>>>>> > Groups
>>>>>>> > "Algorithm Geeks" group.
>>>>>>> > To post to this group, send email to [email protected].
>>>>>>> > To unsubscribe from this group, send email to
>>>>>>> > [email protected].
>>>>>>> > For more options, visit this group at
>>>>>>> > http://groups.google.com/group/algogeeks?hl=en.
>>>>>>> >
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Lalit Kishore Sharma,
>>>>>>> IIIT Allahabad (Amethi Capmus),
>>>>>>> 6th Sem.
>>>>>>>
>>>>>>> --
>>>>>>> You received this message because you are subscribed to the Google
>>>>>>> Groups "Algorithm Geeks" group.
>>>>>>> To post to this group, send email to [email protected].
>>>>>>> To unsubscribe from this group, send email to
>>>>>>> [email protected].
>>>>>>> For more options, visit this group at
>>>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Regards,
>>>>>> Nitish Goyal
>>>>>> Undergraduate Student Of NSIT,
>>>>>> Computer Engineering (B.E.)
>>>>>> Asst. PlaceComm 2011
>>>>>> contact me:- +91-9999605824
>>>>>> Reply at :-  [email protected]
>>>>>>
>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Algorithm Geeks" group.
>>>>>> To post to this group, send email to [email protected].
>>>>>> To unsubscribe from this group, send email to
>>>>>> [email protected].
>>>>>> For more options, visit this group at
>>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Cheers
>>>>> Naveen Kumar
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Algorithm Geeks" group.
>>>>> To post to this group, send email to [email protected].
>>>>> To unsubscribe from this group, send email to
>>>>> [email protected].
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Algorithm Geeks" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected].
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>>
>>>
>>> --
>>> Cheers
>>> Naveen Kumar
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>
>
>
> --
> Regards,
> Nitish Goyal
> Undergraduate Student Of NSIT,
> Computer Engineering (B.E.)
> Asst. PlaceComm 2011
> contact me:- +91-9999605824
> Reply at :-  [email protected]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to