Solved

Forcing Processes into different states

Posted on 2003-11-12
5
251 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

786 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