Sponsored Content
Top Forums UNIX for Advanced & Expert Users forks....HELP!!! someone anyone? Post 49624 by richardspence2 on Tuesday 6th of April 2004 03:36:31 PM
Old 04-06-2004
Question fork...help

Hey,

check out this code... I tried running it with "fgets" as it states that "gets" is a dangerous code.... however, its not executing the linux commands... what could be the problem, or what in this code needs modification??


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>

#define MAX 1024

void parse(char*buf, char ** args);
void execute(char**args);

int main()
{
char buf[MAX];
char *args[64];

for(;Smilie{
/*Prompts for and reads a command*/
printf("Command: ");

if(fgets(buf, MAX, stdin) == '\0'){
printf("\n");
exit(0);
}

/*Splits the strings into arguments*/
parse(buf,args);

/*Executes the command*/
execute(args);
}
}

/* Parse: - Splits the commands in buf into individual arguments */
void parse(char*buf,char **args)
//char * buf;
//char ** args;
{
while(*buf != '\0'){

/*Strips the whitespace with the
use of null so previous argument
is terminated automatically*/

while(*buf == ' '|| *buf == '\t' || buf == '\n')
*buf++ = '\0';

/*Saves the argument*/

*args++ = buf;

/*Skips over the argument*/

while((*buf != '\0') && (buf != ' ') && (*buf != '\t') && (*buf != '\n'))
buf++;
}

*args = '\0';
}


/*Execute spawns the child process
and execute the program*/

void execute(char **args)
//char ** args;
{
pid_t pid;
int status;

/*Get a child process*/

if((pid = fork()) < 0){
perror("ERROR executing fork\n");
exit(1);
}

/*Child executes the code inside the if statement*/

if(pid == 0){
execvp(*args, args);
perror(*args);
exit(1);
}


/*The parent executes the wait*/


while (wait(&status) != pid)
/* empty */ ;

}


Thankx in advance.......
 

5 More Discussions You Might Find Interesting

1. Programming

forks, ipc, fifos, update issues...

Hi, so I've got this program("main") that fork executes another ("user"). These programs communicate through fifos. One communication is a spawn call, where user passes an executable, main forks and executes it. So, I'm keeping track of all my processes using a task table. After the fork (for... (6 Replies)
Discussion started by: Funktar
6 Replies

2. UNIX for Advanced & Expert Users

Question on forks and pipes

I am trying to figure out why when i have the following code int main( { printf("0\n"); fork(); printf("1\n"); exit(0);} and type in the shell a.out | cat the output of this program is 0 1 0 1 instead of 0 1 1 does anyone know? (3 Replies)
Discussion started by: Phantom12345
3 Replies

3. Programming

multiple forks and printf question

Hello *NIX gurus, I have a slight perplexing problem with multiple forks giving different results... Here is the deal. From what I undestand, a fork() call starts executing from the next instruction that follows the fork() call. That means it inherits the PC counter register value of the... (4 Replies)
Discussion started by: navigator
4 Replies

4. Programming

read from file using forks

i'm tring to make 2 processes each read from the same file but only one of them read the file. FILE * fileptr1; fileptr1 = fopen("file1.txt","rt"); pid2=fork(); while(1) { fscanf(fileptr1,"%s",temp1); if(feof(fileptr1)==0) { printf("%i",getpid()); //id of current process ... (6 Replies)
Discussion started by: ddx08
6 Replies

5. Shell Programming and Scripting

Shells, forks, subprocesses... oh my

all, i've been reading to try and get an abstract idea of the process effeciency of commands , sed, bash, perl, awk, find, grep, etc which processes will spawn?, fork?, launch subshell?, etc and under what conditions? how do you know which commands have the faster and better stdio... (2 Replies)
Discussion started by: f77hack
2 Replies
stat.h(3HEAD)							      Headers							     stat.h(3HEAD)

NAME
stat.h, stat - data returned by stat system call SYNOPSIS
#include <sys/types.h> #include <sys/stat.h> DESCRIPTION
The system calls stat, lstat and fstat return data in a stat structure, which is defined in <stat.h>. The constants used in the st_mode field are also defined in this file: #define S_IFMT /* type of file */ #define S_IAMB /* access mode bits */ #define S_IFIFO /* fifo */ #define S_IFCHR /* character special */ #define S_IFDIR /* directory */ #define S_IFNAM /* XENIX special named file */ #define S_INSEM /* XENIX semaphore subtype of IFNAM */ #define S_INSHD /* XENIX shared data subtype of IFNAM */ #define S_IFBLK /* block special */ #define S_IFREG /* regular */ #define S_IFLNK /* symbolic link */ #define S_IFSOCK /* socket */ #define S_IFDOOR /* door */ #define S_ISUID /* set user id on execution */ #define S_ISGID /* set group id on execution */ #define S_ISVTX /* save swapped text even after use */ #define S_IREAD /* read permission, owner */ #define S_IWRITE /* write permission, owner */ #define S_IEXEC /* execute/search permission, owner */ #define S_ENFMT /* record locking enforcement flag */ #define S_IRWXU /* read, write, execute: owner */ #define S_IRUSR /* read permission: owner */ #define S_IWUSR /* write permission: owner */ #define S_IXUSR /* execute permission: owner */ #define S_IRWXG /* read, write, execute: group */ #define S_IRGRP /* read permission: group */ #define S_IWGRP /* write permission: group */ #define S_IXGRP /* execute permission: group */ #define S_IRWXO /* read, write, execute: other */ #define S_IROTH /* read permission: other */ #define S_IWOTH /* write permission: other */ #define S_IXOTH /* execute permission: other */ The following macros are for POSIX conformance (see standards(5)): #define S_ISBLK(mode) block special file #define S_ISCHR(mode) character special file #define S_ISDIR(mode) directory file #define S_ISFIFO(mode) pipe or fifo file #define S_ISREG(mode) regular file #define S_ISSOCK(mode) socket file ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
stat(2), types.h(3HEAD), attributes(5), standards(5) SunOS 5.10 30 Aug 2002 stat.h(3HEAD)
All times are GMT -4. The time now is 12:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy