Solved

Forcing Processes into different states

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 28
Using unmanaged DLL from managed application 14 290
List out all word 7 261
What is atomic operation? 6 47
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

863 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

25 Experts available now in Live!

Get 1:1 Help Now