Login or Register to Ask a Question and Join Our Community


Multi process programming in C


 
Thread Tools Search this Thread
Top Forums Programming Multi process programming in C
# 1  
Old 03-24-2014
Multi process programming in C

So I am trying to learn C and am coding some scripts on my own.

For a start I have decided to port the shell script developed by wisecracker into C.( Here is the link to that script A simple reminder script for beginners to shell scripting. | Unix Linux Forums | OS X (Apple) )

This is what I have done so far:
Code:
/************************************************************************
 *Usage
 *To start : ./reminder -k start -t "Text to remind" -c "repeat time"
 *To stop  : ./remnder -k stop
 */

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sysexits.h>
#include <string.h>


void usage(char * const *arg) {
    printf ("Usage : %s [OPTIONS]\n",arg[0]);
    printf ("        -k start/stop\n");
    printf ("        -t <Reminder text>\n");
    printf ("        -c <countdown(30-300)>\n");
    printf ("        -h Help on usage. ");
    printf ("\n");
    exit(EX_USAGE);
}

void start_routine(char *myname, char *rem, int countdown) {
    FILE *fd = NULL;
    char *script;
    strcpy(script,"./reminder.sh");
    char *content = malloc(1000 * sizeof(char));

    if ( !(fd = fopen(script,"w+")) ) {
        printf("Unable to open/create %s\n",script);
        printf("Error code is %d\n",fd);
        exit(EX_CANTCREAT);
    }
    if (chmod(script,S_IXUSR|S_IRUSR|S_IWUSR|S_IXGRP)) {
        printf("Unable to give execute permission to %s\n",script);
        remove(script);
        exit(EX_CANTCREAT);
    }

    sprintf(content,"#/bin/bash\n");
    sprintf(content,"%sprintf \'\\033[2J\\033[H\'\n",content);
    sprintf(content,"%sprintf '\\033[1m\\033[12;3f%s\\033[0m\\n\\n'\n",content,rem);
    sprintf(content,"%ssleep 5\n",content);
    sprintf(content,"%sexit 0\n",content);

    fwrite(content,sizeof(char),strlen(content),fd);

    fclose(fd);
    free(content);

    while (1) {
        system("xterm -e ./reminder.sh &");
        sleep(countdown);
    }
}

void stop_routine() {
    return;

}

int main(int argc,char * const *argv) {

    char * myname = argv[0];
    int opt;

    int kflag = 0;
    int tflag = 0;
    int cflag = 0;
    char *state;
    char *text;
    int countdown;
    int start = 0;
    int stop = 0;
    pid_t parent_pid,child_pid,pid;
    int status;
    parent_pid=getpid();

    while ((opt = getopt(argc, argv,"hk:t:c:")) != -1 ) {
        switch (opt) {
        case 'k' :
            kflag = 1;
            //DEBUG
            //printf("optarg : %s\n",optarg);
            state = optarg;
            break;
        case 't' :
            tflag = 1;
            text = optarg;
            break;
        case 'c' :
            cflag = 1;
            countdown = atoi(optarg);
            break;
        case 'h' :
            usage (argv);
            break;
            /*    case '?' :
            printf("%s: invalid option.\n",argv[0]);
            usage(argv);
            break;
             */
        }
    }
    //DEBUG
    //printf("%s\n",state);
    if (!kflag) {
        printf ("%s: missing -k option.\n",myname);
        usage(argv);
    } else if (strcmp(state,"start") && strcmp(state,"stop")) {
        printf ("%s: illegel use -k option.\n",myname);
        usage(argv);
    }
    if (!tflag) {
        strcpy(text,"Are you forgetting something?");
    }
    if (!cflag) {
        countdown = 60;
    } else if (countdown < 30 || countdown >300) {
        countdown = 60;
    }


    //Check if the program was invoked to start or stop the applicatio.
    if ( ! strcmp(state,"start")) // if (state == "start")
        start = 1;
    else
        stop = 1;

    printf ("\033[2J\033[H");

    if ( start ) {

        /* The reminder text and the countdown is printed in
         * the main window.
         * This will be done by a child process.
         * We will fork() off now and let the child do the rest of the
         * starting up. So to stop the program we will just kill of the child.
         */
        printf("Starting %s with the below inputs\n",myname);
        printf ("\nReminder text : %s\n",text);
        printf ("Repeat time : %d\n\n",countdown);
        printf ("Run %s with -k stop to stop\n",myname);

        if (( child_pid = fork() ) == -1) {
            perror("fork error");
            exit(EX_UNAVAILABLE);
        }
    } else {
        printf("Stopping %s\n",myname);
        stop_routine();
        exit(EX_OK);
    }

    if (! child_pid) {        //Child process
        start_routine(myname,text,countdown);
    } else {                //Parent process

        if ((pid = wait(&status)) == -1) {
            perror("Wait error\n");
            exit(EX_UNAVAILABLE);
        }
        if (WEXITSTATUS(status)) {
            printf("Reminder has finished exectution.\n");
            return 0;
        } else if (WIFSIGNALED(status)) {
            if ( WTERMSIG(status) == 6 ) {
                printf("Reminder stopped normally using -k stop option\n");
                return 0;
            } else {
                printf("Reminder terminated abnormally by signale: %d\n",
                        WTERMSIG(status));
                exit(EX_UNAVAILABLE);
            }
        }


    }

    return 0;
}

So here is the problems that I am facing now:
1. How do I stop this? I mean how can I make ./reminder -k stop to work? I thought about writing the pid of the parent to file and then using it to stop the program.
2. How can put the parent to background so that I get my stdin back at prompt? Now what happens is that the parent keeps waiting for the child to end, which never happens. What I did was I just removed the wait(&status) and replace it with return 0;. So now the parent will exit and the child will keep on popping up the reminder texts.

Thanks.
# 2  
Old 03-24-2014
The way you're using strcpy is incorrect. Neither char *script nor char *text point to properly allocated memory. Instead, they are uninitialized and pointing to arbitrary locations. You may get lucky, or you may segfault.

char *content points to 1000 bytes, but the code does not prevent a long reminder from overflowing the buffer. In this case, you could get the job done with a single fprintf. Why construct a string in a buffer when you can write the text directly to the stream?

Using atoi in new code is asking for trouble, since it does not perform any type of error checking.

The -k start|stop switch is redundant with the presence of -t. You can set all of the appropriate (int) flags based on the presence or absence of -t.

I did not try to compile the program. I only skimmed the code. There may be other issues lurking. If you haven't done so, enable all of your compilers warnings.

Regards,
Alister
# 3  
Old 03-25-2014
Quote:
Originally Posted by alister
The way you're using strcpy is incorrect. Neither char *script nor char *text point to properly allocated memory. Instead, they are uninitialized and pointing to arbitrary locations. You may get lucky, or you may segfault.
So I need to use malloc first and then do the assign operation. I made the following changes :
Code:
char script[] = "./reminder.sh";

and
Code:
char *text = malloc(1000 * sizeof(char));
    strcpy(text,optarg);

Quote:
Originally Posted by alister
char *content points to 1000 bytes, but the code does not prevent a long reminder from overflowing the buffer. In this case, you could get the job done with a single fprintf. Why construct a string in a buffer when you can write the text directly to the stream?
Ok so I will include a check for the size of the text entered by the user and then if its larger than the allowed limit, I will set it to a default value.

Quote:
Originally Posted by alister
Using atoi in new code is asking for trouble, since it does not perform any type of error checking.
Include a validation for this as well.

Quote:
Originally Posted by alister
The -k start|stop switch is redundant with the presence of -t. You can set all of the appropriate (int) flags based on the presence or absence of -t.
Actually I have made only the -k start/stop option mandatory. The other two are optional. So if the user do not give them, both the reminder text and the countdown will be set to a default value.

Quote:
Originally Posted by alister
I did not try to compile the program. I only skimmed the code. There may be other issues lurking. If you haven't done so, enable all of your compilers warnings.
I enabled all the warnings and I got the below output:
Code:
 gcc -Wall -g -o reminder reminder_1.c
reminder_1.c: In function 'start_routine':
reminder_1.c:34: warning: format '%d' expects type 'int', but argument 2 has type 'struct FILE *'
reminder_1.c:34: warning: format '%d' expects type 'int', but argument 2 has type 'struct FILE *'
reminder_1.c: In function 'main':
reminder_1.c:162: warning: implicit declaration of function 'wait'

For the first warning I did a type casting to int.
Code:
if ( !(fd = fopen(script,"w+")) ) {
        printf("Unable to open/create %s\n",script);
        printf("Error code is %d\n",(int) fd);
        exit(EX_CANTCREAT);
    }

and for the second one I included the sys/wait.h

But still my question remains. How do i make the program stop?
Now I am doing Ctrl-C from the prompt to end the program.
How can I make it stop when I enter ./reminder -k stop

Thanks

---------- Post updated at 06:33 AM ---------- Previous update was at 05:00 AM ----------

So I have made the changes and this is a working copy. And I found an interesting feature. If I give the reminder text as ';rm a;echo ', the file a actually gets removed. So I can give anything between the semi-colon and the script will execute it. Interesting...
Code:
/************************************************************************
 *Usage
 *To start : ./reminder -k start -t "Text to remind" -c "repeat time"
 *To stop  : ./remnder -k stop
 */

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sysexits.h>
#include <string.h>
#include <sys/wait.h>

void usage(char * const *arg) {
    printf ("Usage : %s [OPTIONS]\n",arg[0]);
    printf ("        -k start/stop\n");
    printf ("        -t <Reminder text>\n");
    printf ("        -c <countdown(30-300)>\n");
    printf ("        -h Help on usage. ");
    printf ("\n");
    exit(EX_USAGE);
}

void start_routine(char *myname, char *rem, int countdown) {
    FILE *fd = NULL;
    char script[] = "./reminder.sh";
    //strcpy(script,"./reminder.sh");
    char *content = malloc(1000 * sizeof(char));

    if ( !(fd = fopen(script,"w+")) ) {
        printf("Unable to open/create %s\n",script);
        printf("Error code is %d\n",(int) fd);
        exit(EX_CANTCREAT);
    }
    if (chmod(script,S_IXUSR|S_IRUSR|S_IWUSR|S_IXGRP)) {
        printf("Unable to give execute permission to %s\n",script);
        remove(script);
        exit(EX_CANTCREAT);
    }

    fprintf(fd,"#/bin/bash\n");
    fprintf(fd,"printf \'\\033[2J\\033[H\'\n");
    fprintf(fd,"printf '\\033[1m\\033[12;3f%s\\033[0m\\n\\n'\n",rem);
    fprintf(fd,"sleep 5\n");
    fprintf(fd,"exit 0\n");

    fclose(fd);
    free(content);

    while (1) {
        system("xterm -e ./reminder.sh &");
        sleep(countdown);
    }
}

void stop_routine() {
    return;

}

int main(int argc,char * const *argv) {

    char * myname = argv[0];
    int opt;

    int kflag = 0;
    int tflag = 0;
    int cflag = 0;
    char *state;
    char *text = malloc(100 * sizeof(char));
    int countdown;
    int start = 0;
    int stop = 0;
    pid_t parent_pid,child_pid,pid;
    int status;
    parent_pid=getpid();

    while ((opt = getopt(argc, argv,"hk:t:c:")) != -1 ) {
        switch (opt) {
        case 'k' :
            kflag = 1;
            //DEBUG
            //printf("optarg : %s\n",optarg);
            state = optarg;
            break;
        case 't' :
            tflag = 1;
            strcpy(text,optarg);
            break;
        case 'c' :
            cflag = 1;
            countdown = atoi(optarg);
            break;
        case 'h' :
            usage (argv);
            break;
            /*    case '?' :
            printf("%s: invalid option.\n",argv[0]);
            usage(argv);
            break;
             */
        }
    }
    //DEBUG
    //printf("%s\n",state);
    if (!kflag) {
        printf ("%s: missing -k option.\n",myname);
        usage(argv);
    } else if (strcmp(state,"start") && strcmp(state,"stop")) {
        printf ("%s: illegel use -k option.\n",myname);
        usage(argv);
    }
    if (!tflag) {
        strcpy(text,"Are you forgetting something?");
    }
    if (!cflag) {
        countdown = 60;
    } else if (countdown < 30 || countdown >300) {
        countdown = 60;
    }


    //Check if the program was invoked to start or stop the applicatio.
    if ( ! strcmp(state,"start")) // if (state == "start")
        start = 1;
    else
        stop = 1;

    printf ("\033[2J\033[H");

    if ( start ) {

        /* The reminder text and the countdown is printed in
         * the main window.
         * This will be done by a child process.
         * We will fork() off now and let the child do the rest of the
         * starting up. So to stop the program we will just kill of the child.
         */
        printf("Starting %s with the below inputs\n",myname);
        printf ("\nReminder text : %s\n",text);
        printf ("Repeat time : %d\n\n",countdown);
        printf ("Run %s with -k stop to stop\n",myname);

        if (( child_pid = fork() ) == -1) {
            perror("fork error");
            exit(EX_UNAVAILABLE);
        }
    } else {
        printf("Stopping %s\n",myname);
        stop_routine();
        exit(EX_OK);
    }

    if (! child_pid) {        //Child process
        start_routine(myname,text,countdown);
    } else {                //Parent process

        if ((pid = wait(&status)) == -1) {
            perror("Wait error\n");
            exit(EX_UNAVAILABLE);
        }
        if (WEXITSTATUS(status)) {
            printf("Reminder has finished exectution.\n");
            return 0;
        } else if (WIFSIGNALED(status)) {
            if ( WTERMSIG(status) == 6 ) {
                printf("Reminder stopped normally using -k stop option\n");
                return 0;
            } else {
                printf("Reminder terminated abnormally by signale: %d\n",
                        WTERMSIG(status));
                exit(EX_UNAVAILABLE);
            }
        }


    }

    return 0;
}

# 4  
Old 03-25-2014
As before, I did not compile or test the code. There may be other issues.

content is allocated and freed but never used.

fd will never contain an error code; it's always a pointer value. When that pointer is NULL, it indicates an error and the error code is in errno. That's what you should be inspecting during error handling. To convert the integer errno into a useful message, something like strerror() will help.

As you discovered, inserting arbitrary text into a script can present serious issues. The simplest solution is to write the reminder's text to a separate file.

strcpy of optarg to text is unsafe and can overflow. You can use strncpy, but then must be careful to ensure that the string is always null-terminated. strlcpy is a simpler alternative, if available. However, the simplest alternative in this case is to not copy at all.

Not only do you not need to copy, you don't need to allocate either. The reminder's text has already been allocated storage during startup. It's in argv. The only thing you need to do is pass around the pointer. The default value has also been stored away in the executable's image and it's location can also be passed around.

You implied that you are no longer waiting in the parent, but since the code is still there, a couple of notes about it.

You should always check WIFEXITED before using WEXITSTATUS (as you did with WIFSIGNALED before using WTERMSIG). An implementation is not forbidden from overloading bits for signal and status information (though I don't know if any implementation actually does so).

For portability and readability (especially for readability), it's a far better choice to use the macros in signal.h. Instead of 6, use SIGABRT. As far as I know, even though the kill(1) utility is required to recognize -6 as SIGABRT, nothing requires the kill(2) system call implementation to equate 6 with SIGABRT (although it almost certainly does).

Regarding your interprocess communication question, your pid file idea seems perfectly reasonable.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 03-27-2014
To answer 2, to put the process in background, you will have to
1. fork from the parent.
2. terminate the parent so the child is owned by init.
3. and dissociate the tty by setting session (setsid)

So at this point using the pid file, sounds like a good solution to stop it, since the parent process is dead.
This User Gave Thanks to linuxpenguin For This Post:
# 6  
Old 03-28-2014
I have tried to fix as many as issues I can that has been pointed out by @alister. In this version of the reminder program the parent will exit after fork. The pid of the child that calls the xterm is written into a file which is used to kill the child when needed. And the best thing I got no warnings!! Happly with that Smilie

Now I will try and implement the second approach in which I put the process in background and try to kill the child using it. i have no idea how I am going to do that. First off I will just try to make the parent a background process. Will figure out the rest along the way.
# 7  
Old 03-28-2014
Cool, good luck. I believe when you kill the parent and run setsid, it will disassociate the terminal, thereby putting it in the background.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use the get and post method in the bash ( multi process)?

hi I want to call a lot of links with the post method What to do to speed it up?? ####This method is slow #!/bin/bash func2() { index1=0 while read line ; do index1=$(($index1+1)) url=$line done < tmp/url1.txt } (10 Replies)
Discussion started by: mnnn
10 Replies

2. Programming

Multi head/multi window hello world

I am trying to write a large X app. I have successfully modified my xorg.conf to setup 4 monitors on an NVIDIA Quatro5200. I am trying to modify a simple hello world application to open a window on three of the four monitors. depending on the changes to loop the window creation section and event... (2 Replies)
Discussion started by: advorak
2 Replies

3. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

4. Shell Programming and Scripting

Multi thread shell programming

I have a unix directory where a million of small text files getting accumulated every week. As of now there is a shell batch program in place which merges all the files in this directory into a single file and ftp to other system. Previously the volume of the files would be around 1 lakh... (2 Replies)
Discussion started by: vk39221
2 Replies

5. High Performance Computing

What is it about OpenMosix that supports multi-process applications?

I read that 'Any single program that can run as multiple processes can benefit from OpenMosix: "The GIMP" photo editor and the "kandel" fractal generator are known to do this. Are there other load-balancing clusters that do support multi-process applications? (1 Reply)
Discussion started by: Advice Pro
1 Replies

6. Programming

Redirect Standard Output Multi-Process

Hi, I'm trying to compile the following code: /************** Begin <test.c> ***************/ /* * Compiled with: gcc -Wall -o test test.c */ #include <stdio.h> #include <unistd.h> int main(void) { printf("I'm process %d, son of %d \n", getpid(), getppid()); ... (5 Replies)
Discussion started by: djodjo
5 Replies

7. Programming

Redirect Output Multi-Process

Hi, I'm trying to compile the following code: /************** Begin <test.c> ***************/ /* * Compiled with: gcc -Wall -o test test.c */ #include <stdio.h> #include <unistd.h> int main(void) { printf("I'm process %d, son of %d \n", getpid(), getppid()); printf("Hello \n");... (3 Replies)
Discussion started by: djodjo
3 Replies

8. Programming

message queues and multi-process

Hi, Am supposed to use message queues to send and receive messages between the processes. when i was working on that i realised that the message qid and the message queue related data should be maintained in a shared memory so that it can be accessed by all the processes. Could anybody refer... (10 Replies)
Discussion started by: rvan
10 Replies

9. UNIX for Dummies Questions & Answers

Multi User Multi Task

Dear Experts Why we always hear that unix operating system is Multi User and Multi task. What does these two means. I have looked at some books and documents but couldn't find aclear explenation. Can we say Windows operating system is also multi user and multi task?? Thanks for your help in... (6 Replies)
Discussion started by: Reza Nazarian
6 Replies

10. Shell Programming and Scripting

shell programming for process?

If I want to write program that spread the work to its child process so that each process compute some task what should I do? The objective of my program is to fill in the table which (10*10) in dimension and each column is filled with the fibonacci value of i+j (i mean current row and j mean... (1 Reply)
Discussion started by: robocup
1 Replies
Login or Register to Ask a Question

Featured Tech Videos