Re: [algogeeks] A simple C question

2011-06-05 Thread LALIT SHARMA
@nitish I think using fork() it is not possible , to achieve , what you are trying to do .. Because , though address space is shared , but all the variable of parent process can't be communicated through child process.as they dont have a shared memory block.where we can place our semaphore. It

[algogeeks] A simple C question

2011-06-03 Thread Arpit Mittal
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 :

Re: [algogeeks] A simple C question

2011-06-03 Thread Naveen Kumar
if (!printf(Hello) printf(Hello); else printf(World); On Fri, Jun 3, 2011 at 11:52 AM, Arpit Mittal mrmittalro...@gmail.comwrote: Please help me in this question. What's the condition so that the following code prints both HelloWorld ! if condition printf (Hello); else

Re: [algogeeks] A simple C question

2011-06-03 Thread anand karthik
(!printf(Hello)) On Jun 3, 2011 11:52 AM, Arpit Mittal mrmittalro...@gmail.com 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

Re: [algogeeks] A simple C question

2011-06-03 Thread Vishal Thanki
can use fork() also.. On Fri, Jun 3, 2011 at 11:57 AM, anand karthik anandkarthik@gmail.com wrote: (!printf(Hello)) On Jun 3, 2011 11:52 AM, Arpit Mittal mrmittalro...@gmail.com wrote: Please help me in this question. What's the condition so that the following code prints both

Re: [algogeeks] A simple C question

2011-06-03 Thread Naveen Kumar
Hi Vishal, Can you show us how it be done with fork? On Fri, Jun 3, 2011 at 12:02 PM, Vishal Thanki vishaltha...@gmail.comwrote: can use fork() also.. On Fri, Jun 3, 2011 at 11:57 AM, anand karthik anandkarthik@gmail.com wrote: (!printf(Hello)) On Jun 3, 2011 11:52 AM, Arpit

Re: [algogeeks] A simple C question

2011-06-03 Thread Vishal Thanki
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

Re: [algogeeks] A simple C question

2011-06-03 Thread Naveen Kumar
oh yes, gud one On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki vishaltha...@gmail.comwrote: vishal@ubuntu:~/progs/c\ 12:11:53 PM $ cat fork.c #include stdio.h #include stdlib.h int main() { if (fork()) { printf(hello ); } else {

Re: [algogeeks] A simple C question

2011-06-03 Thread Naveen Kumar
but we can't be sure that it will print hello world or world hello :P On Fri, Jun 3, 2011 at 12:14 PM, Naveen Kumar naveenkumarve...@gmail.comwrote: oh yes, gud one On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki vishaltha...@gmail.comwrote: vishal@ubuntu:~/progs/c\ 12:11:53 PM $ cat

Re: [algogeeks] A simple C question

2011-06-03 Thread Shachindra A C
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 naveenkumarve...@gmail.comwrote: oh

Re: [algogeeks] A simple C question

2011-06-03 Thread Vιиodh
@vishal: can u explain the fork() solution?? On Fri, Jun 3, 2011 at 12:16 PM, Shachindra A C sachindr...@gmail.comwrote: 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

Re: [algogeeks] A simple C question

2011-06-03 Thread Vishal Thanki
@sachindra, @naveen, this was just a plain trick to execute if and else block. i agree with your concerns :) 2011/6/3 Vιиodh vinodh...@gmail.com: @vishal: can u explain the fork()  solution?? On Fri, Jun 3, 2011 at 12:16 PM, Shachindra A C sachindr...@gmail.com wrote: There can be some

Re: [algogeeks] A simple C question

2011-06-03 Thread Naveen Kumar
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 vishaltha...@gmail.comwrote: @sachindra, @naveen, this was just a plain trick to execute

Re: [algogeeks] A simple C question

2011-06-03 Thread Subhransu
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:* subhransu.panigr...@gmail.com On Fri, Jun 3, 2011 at 1:57 PM,

Re: [algogeeks] A simple C question

2011-06-03 Thread nitish goyal
Hi all, I am stuck with this code..Can anyone tell me how to implement semaphores in fork system call Code: #includestdio.h int signal(int *n); int wait(int *n); int main() { int n; n=0; if(fork()) { printf(Hello); signal(n); } else {

Re: [algogeeks] A simple C question

2011-06-03 Thread nicks
@vishal plz help me in understanding fork function...how it is working.?? On Fri, Jun 3, 2011 at 9:58 AM, nitish goyal nitishgoy...@gmail.com wrote: Hi all, I am stuck with this code..Can anyone tell me how to implement semaphores in fork system call Code: #includestdio.h int signal(int

Re: [algogeeks] A simple C question

2011-06-03 Thread LALIT SHARMA
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 nitishgoy...@gmail.com wrote: Hi all, I am stuck with this code..Can anyone tell me how to implement semaphores in fork system call Code: #includestdio.h int

Re: [algogeeks] A simple C question

2011-06-03 Thread nitish goyal
@ 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 lks.ru...@gmail.com 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

Re: [algogeeks] A simple C question

2011-06-03 Thread Naveen Kumar
Process don't share address space when forked. On Fri, Jun 3, 2011 at 10:40 PM, nitish goyal nitishgoy...@gmail.comwrote: @ 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 lks.ru...@gmail.com wrote:

Re: [algogeeks] A simple C question

2011-06-03 Thread nicks
i mean why both the if else statements are working by using fork ? On Fri, Jun 3, 2011 at 10:20 AM, Naveen Kumar naveenkumarve...@gmail.comwrote: Process don't share address space when forked. On Fri, Jun 3, 2011 at 10:40 PM, nitish goyal nitishgoy...@gmail.comwrote: @ Lalit You are

Re: [algogeeks] A simple C question

2011-06-03 Thread Naveen Kumar
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

Re: [algogeeks] A simple C question

2011-06-03 Thread nicks
@naveen.. thanks...i got it finally :) On Fri, Jun 3, 2011 at 10:35 AM, Naveen Kumar naveenkumarve...@gmail.comwrote: 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

Re: [algogeeks] A simple C question

2011-06-03 Thread nitish goyal
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 crazy.logic.k...@gmail.com wrote: @naveen.. thanks...i got it finally :) On Fri, Jun 3, 2011

Re: [algogeeks] A simple C question

2011-06-03 Thread Vishal Thanki
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