Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 14th, 2007, 4:16 AM   #1
dayrinni
Newbie
 
Join Date: Apr 2005
Posts: 17
Rep Power: 0 dayrinni is on a distinguished road
Job Control in a Shell

Hello again,

This assignment is really kicking my butt.

I have to be able to handle the ability to CTRL-Z (suspend) a process and CTRL-C (terminate) a process within the shell I am writing for class. These commands should not affect my shell and the only way that I am able to quit should be using the 'exit' command at the prompt.

So, in theory, I should be able to do start a process on the command line of my shell, say a simple C program that just loops every 1 second and displays output to the screen [KNOWN AS LOOP]. As this is going, I should be able to press CTRL-Z to place it in a suspend mode. Then I should be able to type 'fg 1' and return control. Or I could type CTRL-C instead just to kill it.

How do I do this? Well, this is what I have developed so far. It isn't much really, as my teacher has provided little information on to actually achieve this. Anyways:

1.) I will have to keep track of all of the PIDs the shell process starts up in a list.
2.) I have to overwrite the SIGINT and SIGTSTP signal handlers with the following:
void int_main_handler(int sg)
{
        printf("Shell received an interrupt but we're ignoring it.\n");
        signal(SIGINT, int_main_handler);
}


void sus_main_handler(int sig)
{
        printf("Main shell isn't suspending: pid: %d\n", getpid());
        signal(SIGTSTP, sus_main_handler);
}

This makes it so that CTRL-Z and CTRL-C do not work on my shell prompt.

When a process such as LOOP executes with exec, we should give control to this process which seems to happen by default. Now, if we press CTRL-C it should terminate.

So we can make another signal handler:
void int_handler(int sig)
{
        printf("!!Got interrupt!!\n");
        signal(SIGINT, int_handler);
        exit(2);
}

In our code, in the if statement for the child after the fork, but before the exec, we will add the new signal handler:

pid_t pid;


        pid = fork();

        if (pid < 0)
        {
                fprintf(stderr, "fork failed\n");
                exit(2);

        }
        if (pid == 0)
        {

                set_redirection_descriptors(redirection, file_name);
                setpgrp();

                signal(SIGTSTP, sus_handler);
                signal(SIGINT, int_handler);

                //printf("pid of child is: %d\n", getpid());
                if( execvp(command, arglist) == -1)
                {
                        fprintf(stderr, "execl failed\n");
                        exit(3);

                }
        }

This does not work. I suspect it is because exec will replace the shell program with a copy of itself and in this case: LOOP. So LOOP does not have the signal handler.

This is the output of the above:
Quote:
/u0/users/4/jsimone1/cs552/lab1> ./loop
/u0/users/4/jsimone1/cs552/lab1> Loop program started
^CShell received an interrupt but we're ignoring it.
Command input: ./loop
/u0/users/4/jsimone1/cs552/lab1> SET_REDIRECTION_DESC: Input file name: .
Loop program started
So the problem is: how do we make the new process know of this signal handler?


I have to be able to handle CTRL-Z.

The signal handle will be added as usual:

void sus_handler(int sig)
{
        printf("SUSPENDED: pid: %d\n", getpid());

        signal(SIGTSTP , sus_main_handler);
}


If I run LOOP, it seems to work with the above handler for Suspend. Previously, it was not working at all. Very strange. So I guess that magically fixed itself. I was just trying to get it to work for like 3 hours, haha. It'll probably break on me again...haha.

3.) When a process ends, how can we have the parent know that it terminated and what PID it was? This terminated PID will need to be removed the shell's PID list for the jobs.

I think the above are the main problems I'm having with this.

Thanks for any help as usual.
dayrinni is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Version control Mad_guy Coder's Corner Lounge 6 Jul 18th, 2007 3:46 PM
How to add inherited control to toolbox. InfoGeek C# 3 Feb 8th, 2007 10:12 PM
questions on windows sendMessage and RichEdit control nomer C++ 1 Mar 3rd, 2006 3:51 AM
C programing control of the Serial Port. Light C++ 5 Feb 24th, 2005 3:14 PM
Show web user control hidden see07 C# 1 Feb 2nd, 2005 11:35 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC