Solved

Forcing Processes into different states

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to design, and implement simple interface 5 138
C dll call freezes 5 107
Finding a good hash function 4 127
valid enum? 6 75
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.

828 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