Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:27:55+00:00 2026-06-15T10:27:55+00:00

I am working on some code to create a process that goes blocked and

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:27:56+00:00Added an answer on June 15, 2026 at 10:27 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some code that reads from a socket, and it goes wrong
I am working on some code, basically I have a form. On that form
I'm working some code that inserts csv rows into an SQLite database using Python.
I'm working with some code that I've found online and I don't quite understand
I have been working on some code to submit an uploaded image to PHP
I'm working on some code that takes a hexadecimal string input and produces the
I am working on some code that I inherited from a programmer who is
What I think I need to do is create some code that lets me
I have some existing code making use of System.Diagnostics.Process.Start to start a child process.
I have a .net file that utilizes a c# to process some form(post) information,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.