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 would be better if you use , a global variable and 2 threads and
try to show how semaphores work.

Correct Me , if I am wrong ..!!

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 at 10:35 AM, Naveen Kumar naveenkumarve...@gmail.com
 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 crazy.logic.k...@gmail.com 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
 naveenkumarve...@gmail.com wrote:

 Process don't share address space when forked.


 On Fri, Jun 3, 2011 at 10:40 PM, nitish goyal nitishgoy...@gmail.com
 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 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 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 *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
  subhransu.panigr...@gmail.com
  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: subhransu.panigr...@gmail.com
 
 
  On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar
  naveenkumarve...@gmail.com
  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
  vishaltha...@gmail.com
  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 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 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.com
   wrote:
  
   oh yes,
   gud one
  
   On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
   vishaltha...@gmail.com
   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
   naveenkumarve...@gmail.com wrote:
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.com
wrote:
   
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
 HelloWorld !

 if condition
 printf (Hello);
 else
 printf(World);




 --
 -Arpit Mittal
 6th Semester,
 Indian Institute of 

[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 : arpitmittal.ii...@gmail.com
   rit2008...@iiita.ac.in
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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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
   printf(World);




 --
 -Arpit Mittal
 6th Semester,
 Indian Institute of Information Technology,Allahabad
 Email : arpitmittal.ii...@gmail.com
rit2008...@iiita.ac.in
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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 Institute of Information Technology,Allahabad
 Email : arpitmittal.ii...@gmail.com
 rit2008...@iiita.ac.in
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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 HelloWorld !

 if condition
 printf (Hello);
 else
 printf(World);




 --
 -Arpit Mittal
 6th Semester,
 Indian Institute of Information Technology,Allahabad
 Email : arpitmittal.ii...@gmail.com
 rit2008...@iiita.ac.in
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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 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 Institute of Information Technology,Allahabad
  Email : arpitmittal.ii...@gmail.com
  rit2008...@iiita.ac.in
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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
vishal@ubuntu:~/progs/c\ 12:12:06 PM $ ./a.out
hello world


On Fri, Jun 3, 2011 at 12:09 PM, Naveen Kumar
naveenkumarve...@gmail.com wrote:
 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.com
 wrote:

 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
  HelloWorld !
 
  if condition
  printf (Hello);
  else
  printf(World);
 
 
 
 
  --
  -Arpit Mittal
  6th Semester,
  Indian Institute of Information Technology,Allahabad
  Email : arpitmittal.ii...@gmail.com
  rit2008...@iiita.ac.in
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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 {
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
 naveenkumarve...@gmail.com wrote:
  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.com
  wrote:
 
  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
   HelloWorld !
  
   if condition
   printf (Hello);
   else
   printf(World);
  
  
  
  
   --
   -Arpit Mittal
   6th Semester,
   Indian Institute of Information Technology,Allahabad
   Email : arpitmittal.ii...@gmail.com
   rit2008...@iiita.ac.in
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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 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
 naveenkumarve...@gmail.com wrote:
  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.com
  wrote:
 
  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
   HelloWorld !
  
   if condition
   printf (Hello);
   else
   printf(World);
  
  
  
  
   --
   -Arpit Mittal
   6th Semester,
   Indian Institute of Information Technology,Allahabad
   Email : arpitmittal.ii...@gmail.com
   rit2008...@iiita.ac.in
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 Cheers
 Naveen Kumar




-- 
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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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 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 {
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
 naveenkumarve...@gmail.com wrote:
  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.com
  wrote:
 
  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
   HelloWorld !
  
   if condition
   printf (Hello);
   else
   printf(World);
  
  
  
  
   --
   -Arpit Mittal
   6th Semester,
   Indian Institute of Information Technology,Allahabad
   Email : arpitmittal.ii...@gmail.com
   rit2008...@iiita.ac.in
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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 of execution unless we use semaphores.


 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 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
 naveenkumarve...@gmail.com wrote:
  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.com
 
  wrote:
 
  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
   HelloWorld !
  
   if condition
   printf (Hello);
   else
   printf(World);
  
  
  
  
   --
   -Arpit Mittal
   6th Semester,
   Indian Institute of Information Technology,Allahabad
   Email : arpitmittal.ii...@gmail.com
   rit2008...@iiita.ac.in
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



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 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.com
 wrote:

 oh yes,
 gud one

 On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki vishaltha...@gmail.com
 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
 naveenkumarve...@gmail.com wrote:
  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.com
  wrote:
 
  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
   HelloWorld !
  
   if condition
   printf (Hello);
   else
   printf(World);
  
  
  
  
   --
   -Arpit Mittal
   6th Semester,
   Indian Institute of Information Technology,Allahabad
   Email : arpitmittal.ii...@gmail.com
   rit2008...@iiita.ac.in
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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 

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 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 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.com
  wrote:
 
  oh yes,
  gud one
 
  On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki vishaltha...@gmail.com
 
  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
  naveenkumarve...@gmail.com wrote:
   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.com
   wrote:
  
   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
HelloWorld !
   
if condition
printf (Hello);
else
printf(World);
   
   
   
   
--
-Arpit Mittal
6th Semester,
Indian Institute of Information Technology,Allahabad
Email : arpitmittal.ii...@gmail.com
rit2008...@iiita.ac.in
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 algogeeks@googlegroups.com
 .
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
 
 
  --
  With regards,
  Vιиodh
 
  --
  You received this message 

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, Naveen Kumar naveenkumarve...@gmail.comwrote:

 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 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 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.com
  wrote:
 
  oh yes,
  gud one
 
  On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki 
 vishaltha...@gmail.com
  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
  naveenkumarve...@gmail.com wrote:
   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.com
   wrote:
  
   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
HelloWorld !
   
if condition
printf (Hello);
else
printf(World);
   
   
   
   
--
-Arpit Mittal
6th Semester,
Indian Institute of Information Technology,Allahabad
Email : arpitmittal.ii...@gmail.com
rit2008...@iiita.ac.in
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
 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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 algogeeks@googlegroups.com
 .
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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 algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  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
 

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
{
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 subhransu.panigr...@gmail.comwrote:

 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, Naveen Kumar 
 naveenkumarve...@gmail.comwrote:

 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 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 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.com
  wrote:
 
  oh yes,
  gud one
 
  On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki 
 vishaltha...@gmail.com
  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
  naveenkumarve...@gmail.com wrote:
   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.com
   wrote:
  
   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
HelloWorld !
   
if condition
printf (Hello);
else
printf(World);
   
   
   
   
--
-Arpit Mittal
6th Semester,
Indian Institute of Information Technology,Allahabad
Email : arpitmittal.ii...@gmail.com
rit2008...@iiita.ac.in
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
 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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
 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 

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 *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 
 subhransu.panigr...@gmail.comwrote:

 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, Naveen Kumar 
 naveenkumarve...@gmail.comwrote:

 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 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 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.com
  wrote:
 
  oh yes,
  gud one
 
  On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki 
 vishaltha...@gmail.com
  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
  naveenkumarve...@gmail.com wrote:
   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.com
   wrote:
  
   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
HelloWorld !
   
if condition
printf (Hello);
else
printf(World);
   
   
   
   
--
-Arpit Mittal
6th Semester,
Indian Institute of Information Technology,Allahabad
Email : arpitmittal.ii...@gmail.com
rit2008...@iiita.ac.in
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
 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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
 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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 algogeeks@googlegroups.com
 .
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this 

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 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 subhransu.panigr...@gmail.com
 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: subhransu.panigr...@gmail.com


 On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar naveenkumarve...@gmail.com
 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 vishaltha...@gmail.com
 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 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 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.com
  wrote:
 
  oh yes,
  gud one
 
  On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
  vishaltha...@gmail.com
  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
  naveenkumarve...@gmail.com wrote:
   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.com
   wrote:
  
   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
HelloWorld !
   
if condition
printf (Hello);
else
printf(World);
   
   
   
   
--
-Arpit Mittal
6th Semester,
Indian Institute of Information Technology,Allahabad
Email : arpitmittal.ii...@gmail.com
rit2008...@iiita.ac.in
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
algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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
algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   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 algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, 

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 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 *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 subhransu.panigr...@gmail.com
 
  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: subhransu.panigr...@gmail.com
 
 
  On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar 
 naveenkumarve...@gmail.com
  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 vishaltha...@gmail.com
 
  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 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 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.com
   wrote:
  
   oh yes,
   gud one
  
   On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
   vishaltha...@gmail.com
   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
   naveenkumarve...@gmail.com wrote:
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.com
wrote:
   
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
 HelloWorld !

 if condition
 printf (Hello);
 else
 printf(World);




 --
 -Arpit Mittal
 6th Semester,
 Indian Institute of Information Technology,Allahabad
 Email : arpitmittal.ii...@gmail.com
 rit2008...@iiita.ac.in
 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
 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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
 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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
 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at

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:

 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 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 
 subhransu.panigr...@gmail.com
  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: subhransu.panigr...@gmail.com
 
 
  On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar 
 naveenkumarve...@gmail.com
  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 
 vishaltha...@gmail.com
  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 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 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.com
   wrote:
  
   oh yes,
   gud one
  
   On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
   vishaltha...@gmail.com
   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
   naveenkumarve...@gmail.com wrote:
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.com
wrote:
   
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
 HelloWorld !

 if condition
 printf (Hello);
 else
 printf(World);




 --
 -Arpit Mittal
 6th Semester,
 Indian Institute of Information Technology,Allahabad
 Email : arpitmittal.ii...@gmail.com
 rit2008...@iiita.ac.in
 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
 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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
 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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
 algogeeks@googlegroups.com.
To 

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 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.comwrote:

 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 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 
 subhransu.panigr...@gmail.com
  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: subhransu.panigr...@gmail.com
 
 
  On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar 
 naveenkumarve...@gmail.com
  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 
 vishaltha...@gmail.com
  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 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 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.com
   wrote:
  
   oh yes,
   gud one
  
   On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
   vishaltha...@gmail.com
   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
   naveenkumarve...@gmail.com wrote:
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.com
wrote:
   
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
 HelloWorld !

 if condition
 printf (Hello);
 else
 printf(World);




 --
 -Arpit Mittal
 6th Semester,
 Indian Institute of Information Technology,Allahabad
 Email : arpitmittal.ii...@gmail.com
 rit2008...@iiita.ac.in
 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
 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 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
 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

   
--
You received this message because 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 part and parent will execute if place.



On Fri, Jun 3, 2011 at 10:58 PM, nicks crazy.logic.k...@gmail.com 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 
 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 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.comwrote:

 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 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 
 subhransu.panigr...@gmail.com
  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: subhransu.panigr...@gmail.com
 
 
  On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar 
 naveenkumarve...@gmail.com
  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 
 vishaltha...@gmail.com
  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 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 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.com
   wrote:
  
   oh yes,
   gud one
  
   On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
   vishaltha...@gmail.com
   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
   naveenkumarve...@gmail.com wrote:
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.com
wrote:
   
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
 HelloWorld !

 if condition
 printf (Hello);
 else
 printf(World);




 --
 -Arpit Mittal
 6th Semester,
 Indian Institute of Information Technology,Allahabad
 Email : arpitmittal.ii...@gmail.com
 rit2008...@iiita.ac.in
 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
 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


 --
 You received this 

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 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 crazy.logic.k...@gmail.com 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 
 naveenkumarve...@gmail.com wrote:

 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.comwrote:

 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 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 
 subhransu.panigr...@gmail.com
  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: subhransu.panigr...@gmail.com
 
 
  On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar 
 naveenkumarve...@gmail.com
  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 
 vishaltha...@gmail.com
  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 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 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.com
   wrote:
  
   oh yes,
   gud one
  
   On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
   vishaltha...@gmail.com
   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
   naveenkumarve...@gmail.com wrote:
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.com
wrote:
   
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
 HelloWorld !

 if condition
 printf (Hello);
 else
 printf(World);




 --
 -Arpit Mittal
 6th Semester,
 Indian Institute of Information Technology,Allahabad
 Email : arpitmittal.ii...@gmail.com
 rit2008...@iiita.ac.in
 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
 algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For 

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 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 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 crazy.logic.k...@gmail.comwrote:

 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.com wrote:

 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.comwrote:

 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 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 
 subhransu.panigr...@gmail.com
  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: subhransu.panigr...@gmail.com
 
 
  On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar 
 naveenkumarve...@gmail.com
  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 
 vishaltha...@gmail.com
  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 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 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.com
   wrote:
  
   oh yes,
   gud one
  
   On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
   vishaltha...@gmail.com
   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
   naveenkumarve...@gmail.com wrote:
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.com
wrote:
   
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
 HelloWorld !

 if condition
 printf (Hello);
 else
 printf(World);




 --
 -Arpit Mittal
 6th Semester,
 Indian Institute of Information Technology,Allahabad
 Email : arpitmittal.ii...@gmail.com
 rit2008...@iiita.ac.in
 Contact : +91-8853049787

 Let every man be respected as an individual and no man
 idolized.

 --
 You received this message because you are subscribed
 

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 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 nitishgoy...@gmail.com 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 crazy.logic.k...@gmail.com wrote:

 @naveen.. thanks...i got it finally :)

 On Fri, Jun 3, 2011 at 10:35 AM, Naveen Kumar naveenkumarve...@gmail.com
 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 crazy.logic.k...@gmail.com
 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
 naveenkumarve...@gmail.com wrote:

 Process don't share address space when forked.


 On Fri, Jun 3, 2011 at 10:40 PM, nitish goyal nitishgoy...@gmail.com
 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 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 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 *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
  subhransu.panigr...@gmail.com
  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: subhransu.panigr...@gmail.com
 
 
  On Fri, Jun 3, 2011 at 1:57 PM, Naveen Kumar
  naveenkumarve...@gmail.com
  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
  vishaltha...@gmail.com
  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 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 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.com
   wrote:
  
   oh yes,
   gud one
  
   On Fri, Jun 3, 2011 at 12:12 PM, Vishal Thanki
   vishaltha...@gmail.com
   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
   naveenkumarve...@gmail.com wrote:
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.com
wrote:
   
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