1

I am working on some code to create a process that goes blocked and then ends, I have to be able to see the blocked state with ps.

I tried with this, but my C knowledge is not good. The code doesn't print anything.

Here it is:

#include <stdio.h> #include <stdlib.h> //exit(); #include <unistd.h> //sleep(); int main(int argc, char *argv[]) { createblocked(); } int pid; int i; int estado; void createblocked() { pid = fork(); switch( pid ) { case -1: // pid -1 error ocurred perror("error\n"); break; case 0: // pid 0 means its the child process sleep(); // we put the child to sleep so the parent will be blocked. printf("child sleeping..."); break; default: // !=0 parent process // wait function puts parent to wait for the child // the child is sleeping so the parent will be blocked wait( estado ); printf("parent waiting...\n"); printf("Child terminated.\n"); break; } exit(0); } 

It should be easy because its only a little program that goes blocked, but I am walking in circles I think. Any advice?

13
  • 2
    I hope this is linux/unix, so compile it with gcc -Wall -Werror ... first. Commented Dec 2, 2012 at 17:20
  • 4
    Doesn't sleep take a parameter? Commented Dec 2, 2012 at 17:21
  • 1
    And btw. the argument to wait() is int *, not int... Commented Dec 2, 2012 at 17:24
  • 1
    you missed a \n on the child sleeping printf, you may also want to add fflush(stdout) after every print and move printf("parent waiting...\n"); before the wait. Commented Dec 2, 2012 at 17:24
  • 1
    Instead of sleep(xxx) you could use pause(). Pause is the ultimate blocking system call; since ia basically does nothing. Commented Dec 2, 2012 at 17:25

2 Answers 2

3

sleep() takes a parameter: the number of seconds to sleep. When you omit it, it tends to return immediately.

Also wait() takes an int *, not an int.

try this:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]) { createblocked(); } int pid; int i; int estado; void createblocked() { pid = fork(); switch(pid) { case -1: // pid -1 error ocurred perror("error\n"); break; case 0: // pid 0 means its the child process printf("child sleeping...\n"); sleep(500); // we put the child to sleep so the parent will be blocked. break; default: // !=0 parent process // wait function puts parent to wait for the child // thechild is sleeping so the parent will be blocked printf("parent waiting...\n"); wait(&estado); printf("Child terminated.\n"); break; } exit(0); } 

note: I also moved the printf("parent waiting...\n") above the call to wait(), so you should see it before the parent blocks waiting on the child.

edit: Also, include <unistd.h>. While not strictly required in order for the program to work (on most systems), doing so will give you better compile-time error reporting for things like missing and/or incorrectly-typed function arguments.

Sign up to request clarification or add additional context in comments.

1 Comment

What @Aubin says is true. If you want to see "child sleeping..." you'll need to move that printf before the sleep() call as well. I'll make the edit in my example above.
1

man sleep

man wait

You should give the number of seconds as an argument in sleep().

For wait and sleep include <unistd.h>

1 Comment

@Rkan : got your output or still have a problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.