Processes problem - Need help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Processes problem - Need help
# 1  
Old 04-18-2015
Processes problem - Need help

Hello,
I can't figure out how to solve this problem. I know that is not something very difficult, but it doesn't work for me. Any help is important for me, thank you!
For each command line argument, the main process will launch one subprocess.
Each such process will establish if the assigned argument is a director, file or something else.
Based on the established type the subprocess will return to the parent process the followings:
- if the argument is a file then it will return the file size (see st_size from stat).
- if it is a directory then it will return the number of text files (established using file command) found in this directory
or among his subdirectories. To establish the number of text files the process will use popen to execute a shell script.
- for cases where the argument is neither a file or a directory it will return a random generated number between 5 and 15.
The communication between the process and his sub processes will be done using a private pipe channel.
Before each read or write pipe operation each process will print what is written or was read from the pipe.

What I did:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <string.h>

main(int argc, char* argv[]) {
    int nr, i, fd[2];
    pipe(fd);   //make the pipe 
    struct stat st;
    struct stat s; 
    FILE *f;
    FILE *f2; 
    int status;
    char path[50];

//start parsing the arguments 
for (i = 1; i < argc; i++) {
//creating a subprocess
    if (fork() == 0) {

        if(stat(argv[i],&s)==0)
        {     //testing if file 
            if (s.st_mode & S_IFREG)
            {
                stat(argv[i],&st);
                nr=st.st_size;  
                printf("size of file %s is %d\n",argv[i],nr);   
                write(fd[1],&nr,sizeof(int));    
                close(fd[0]);
                close(fd[1]);

            }
            else if(s.st_mode & S_IFDIR) //testing if directory
            { 
                f=popen("find argv[i] -type f | wc -l", "r");
                if (f==NULL){
                    printf("popen error");
                }
                if (fgets(path,10,f)!=NULL){
                    fscanf(f,"%d",&nr); 
                }
                status=pclose(f);
                if (status==-1){
                    printf("error closing popen");
                }                
                printf("nr of files in directory %s is %d\n",argv[i],nr);   
                write(fd[1],&nr,sizeof(int));
                close(fd[0]);
                close(fd[1]);

            }
        }   
       else
       {
              //getting a random generated number 
            nr = rand() % 15 + 5; 
            printf("a random generated number: %d\n",nr);
            write(fd[1],&nr,sizeof(int));
            close(fd[0]);
            close(fd[1]);   
       }

        // here ends every subprocess 
    }
//closing the pipe 
close(fd[0]);
close(fd[1]);
}

// parent receives what the subprocesses sent 
for (i = 1; i < argc; i++) {
    read(fd[0],&nr,sizeof(int));
    printf("the parent received: %d\n",nr);
    close(fd[0]);
    close(fd[1]);
}    
close(fd[0]);
close(fd[1]);
return 0;
}

When I run it, it prints the message from the parent multiple times and if a directory is passed as argument I get the error: find:argv[i]': No such file or directory`. Also it prints multiple times some of the messages from the subprocesses...

Last edited by Ruinum; 04-18-2015 at 11:33 AM.. Reason: adding info
# 2  
Old 04-18-2015
Moderator's Comments:
Mod Comment Homework problems need to be submitted in the Homework and Coursework forum with a completely filled out homework template.

This thread is closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Problem listing processes

hi! i wrote this script(ubuntu os): clear echo "number of running processes" ps -ef | wc -lbut i can't get the number of my currently running processes... is there a way to see HOW MANY processes are running to my system? ---------- Post updated at 11:20 PM ---------- Previous update... (2 Replies)
Discussion started by: strawhatluffy
2 Replies

2. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

3. Solaris

Identifying and grouping OS processes and APP processes

Hi Is there an easy way to identify and group currently running processes into OS processes and APP processes. Not all applications are installed as packages. Any free tools or scripts to do this? Many thanks. (2 Replies)
Discussion started by: wilsonee
2 Replies

4. Shell Programming and Scripting

processes problem...

The called program gets as parameters two or more integers and and returns 0 if all pairs of two are relatively primes, and 1 otherwise. The callee shall read a sequence of numbers and tell whether they two by tworelatively primes. (1 Reply)
Discussion started by: baku
1 Replies

5. UNIX for Advanced & Expert Users

Monitoring Processes - Killing hung processes

Is there a way to monitor certain processes and if they hang too long to kill them, but certain scripts which are expected to take a long time to let them go? Thank you Richard (4 Replies)
Discussion started by: ukndoit
4 Replies

6. UNIX for Dummies Questions & Answers

Processes

Can someone tell how to find out how long a process has been running? I want to find something that is usually there that the paging system can check on. Thanks (4 Replies)
Discussion started by: Beetlejuice
4 Replies

7. Filesystems, Disks and Memory

processes

write a program create two processes to run a for loop which adds numbers 1 to n , say one process adds odd numbers and other adds even numbers (1 Reply)
Discussion started by: jayaram_miryabb
1 Replies

8. Shell Programming and Scripting

I need some example of Co-Processes

I want to know how to work the Co-Processes in kornshell scripts. So, I very need some script about Co-Processes! thanks ...:) (3 Replies)
Discussion started by: javalee
3 Replies

9. UNIX for Dummies Questions & Answers

processes

What command string will locate ONLY the PID of a process and ouput only the number of PID of the process? (1 Reply)
Discussion started by: mma_buc_98
1 Replies

10. UNIX for Dummies Questions & Answers

co-processes

Is it possible to have a main script (i will call it main.ksh) that executes say, 4 other scripts (sub_prog_1.ksh, sub_prog_2.ksh etc..) from within this main.ksh (simultaneously/in parallel), have them run in the background and communicate back to main.ksh when complete? My guess is to use... (1 Reply)
Discussion started by: google
1 Replies
Login or Register to Ask a Question