forks....HELP!!! someone anyone?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users forks....HELP!!! someone anyone?
# 1  
Old 03-26-2004
Question forks....HELP!!! someone anyone?

Hey guys,

I'm given this bit of code, but, I'm having some problems executing it with the functions I've defined so far. I'm suppose to define the funtions "parse" and "execute." Parse splits the command in buf into individual arguments. It strips whitespace, replacing those it finds with NULLS thereby defining where an argument ends.

How would I create the function execute using 'execvp' which creates a child process with the commands developed in the function parse?


The code is:
.................................................................................................... .....

#define MAX 512



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

void execute(char** args);



int main()

{

char buf[MAX];

char* args[64];



for ( ; ; )

{

/* prompt for a read a command */

printf(“Command: “);

if(fgets(buf, MAX,stdin) == ‘\0')

{

printf(“\n”);

exit(0);

}



/* Split the command string into arguments */

parse(buf, args);



/* Execute the command. */

execute(args);

}



return 0;

}
.................................................................................................... .....

Thankx in advance.....
# 2  
Old 03-27-2004
forks!...more probs..HELP

Hey,

Having followed what you said, this is what I was able to create.
However, its not executing the commands thru the terminal. Please take a look at it and let me know what you think please.

****************************************************
#include <stdio.h>

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

for (;Smilie {
/*
* Prompt for and read a command.
*/
printf("Command: ");

if (gets(buf) == NULL) {
printf("\n");
exit(0);
}

/*
* Split the string into arguments.
*/
parse(buf, args);

/*
* Execute the command.
*/
execute(args);
}
}

/*
* parse--split the command in buf into
* individual arguments.
*/
parse(buf, args)
char *buf;
char **args;
{
while (*buf != NULL) {
/*
* Strip whitespace
*/
while ((*buf == ' ') || (*buf == '\t'))
*buf++ = NULL;

/*
* Save the argument.
*/
*args++ = buf;

/*
* Skip over the argument.
*/
while ((*buf != NULL) && (*buf != ' ') && (*buf ! '\t'))
buf++;
}

*args = NULL;
}

/*
* execute--spawn a child process and execute
* the program.
*/
execute(args)
char **args;
{
int pid, status;

/*
* Get a child process.
*/
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}

/*
* The child executes the code inside the if.
*/
if (pid == 0) {
execvp(*args, args);
perror(*args);
exit(1);
}

/*
* The parent executes the wait.
*/
while (wait(&status) != pid)
/* empty */ ;
}
****************************************************

Thanks in advance
# 3  
Old 03-27-2004
Thanks for the input. Actually, the code is more Kernighan & Ritchie as opposed to ANSI C as K&R is the book am using as my guide along with Unix Systems Programming SVR 4.

I'm yet to implement the further instructions you've given me as am not at a terminal just now. So, whenever I get that done, I'll let you know what I've formulated.

By the way, are you familiar with the Java environment? I have a question on graphics I want to ask you.
# 4  
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.......
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. 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

2. 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

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. 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

5. 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
Login or Register to Ask a Question