Solved

Forcing Processes into different states

Posted on 2003-11-12
5
229 Views
Last Modified: 2010-04-15
I basically just want to make one or two programs that will create a total of 4 processes, each process being in a running, sleeping, stopped, or zombie state. I would like to get one process in each state. I have been able to get a sleeping and zombie state already, but am not quite sure how to show a running or stopped state. I cannot use CTRL-Z to suspend the program to get the stopped state, the process has to enter that on its own. Any suggestions? Here is what I got already:

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
  pid_t childPID;
  pid_t returnPID;
  int   status;

  childPID = fork();
  if (childPID != 0) {
    printf("Parent issues wait()\n");
    sleep(10*6);
    returnPID = wait(&status);
    printf("PID returned by wait() = %d \n", returnPID);
    if (WIFEXITED(status)) {
      printf("status returned by wait() = %d\n", WEXITSTATUS(status));
    }
  } else {
    printf("                            Child: PID  = %d \n", getpid());
    printf("                            Child will sleep for 20 seconds.\n");
    printf("                            Child: returnCode = %d\n", returnCode);
    printf("                            Child process will terminate.\n");
  }
  printf("Parent process will terminate.\n");
}


Thanks for any help.
0
Comment
Question by:Shiromagius
  • 3
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9729418
raise ( SIGSTOP);
or
kill(getpid(),SIGSTOP)
0
 

Author Comment

by:Shiromagius
ID: 9729501
That worked great for stopped sunnycoder. Now what about the running state? I think my processes execute too fast because everytime i do ps i see them in sleeping or now stopped mode :)
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 100 total points
ID: 9729561
Now what about the running state?

for ( i = 0; i < 10000 ; i++ )
    for ( j = 0; i < 10000 ; i++ )
         for ( k = 0; i < 10000 ; i++ )
              for ( l = 0; i < 10000 ; i++ );

dont complain that it hung ;o)
reduce the number of loops as well as loop counters if it takes too long
0
 

Author Comment

by:Shiromagius
ID: 9729647
Thank you very much. And your quick reply was very helpful. Thanks a lot sunnycoder, points awarded :D
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9729670
glad to be of help :o)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now