Sponsored Content
Top Forums UNIX for Advanced & Expert Users forks....HELP!!! someone anyone? Post 49191 by richardspence2 on Saturday 27th of March 2004 02:36:03 PM
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
 

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
uplevel(n)						       Tcl Built-In Commands							uplevel(n)

__________________________________________________________________________________________________________________________________________________

NAME
uplevel - Execute a script in a different stack frame SYNOPSIS
uplevel ?level? arg ?arg ...? _________________________________________________________________ DESCRIPTION
All of the arg arguments are concatenated as if they had been passed to concat; the result is then evaluated in the variable context indi- cated by level. Uplevel returns the result of that evaluation. If level is an integer then it gives a distance (up the procedure calling stack) to move before executing the command. If level consists of # followed by a number then the number gives an absolute level number. If level is omitted then it defaults to 1. Level cannot be defaulted if the first command argument starts with a digit or #. For example, suppose that procedure a was invoked from top-level, and that it called b, and that b called c. Suppose that c invokes the uplevel command. If level is 1 or #2 or omitted, then the command will be executed in the variable context of b. If level is 2 or #1 then the command will be executed in the variable context of a. If level is 3 or #0 then the command will be executed at top-level (only global variables will be visible). The uplevel command causes the invoking procedure to disappear from the procedure calling stack while the command is being executed. In the above example, suppose c invokes the command uplevel 1 {set x 43; d} where d is another Tcl procedure. The set command will modify the variable x in b's context, and d will execute at level 3, as if called from b. If it in turn executes the command uplevel {set x 42} then the set command will modify the same variable x in b's context: the procedure c does not appear to be on the call stack when d is executing. The info level command may be used to obtain the level of the current procedure. Uplevel makes it possible to implement new control constructs as Tcl procedures (for example, uplevel could be used to implement the while construct as a Tcl procedure). The namespace eval and apply commands offer other ways (besides procedure calls) that the Tcl naming context can change. They add a call frame to the stack to represent the namespace context. This means each namespace eval command counts as another call level for uplevel and upvar commands. For example, info level 1 will return a list describing a command that is either the outermost procedure call or the out- ermost namespace eval command. Also, uplevel #0 evaluates a script at top-level in the outermost namespace (the global namespace). EXAMPLE
As stated above, the uplevel command is useful for creating new control constructs. This example shows how (without error handling) it can be used to create a do command that is the counterpart of while except for always performing the test after running the loop body: proc do {body while condition} { if {$while ne "while"} { error "required word missing" } set conditionCmd [list expr $condition] while {1} { uplevel 1 $body if {![uplevel 1 $conditionCmd]} { break } } } SEE ALSO
apply(n), namespace(n), upvar(n) KEYWORDS
context, level, namespace, stack frame, variables Tcl uplevel(n)
All times are GMT -4. The time now is 06:59 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy