Solved

Forcing Processes into different states

Posted on 2003-11-12
5
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

729 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