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
el(1)							      General Commands Manual							     el(1)

NAME
el -- program to make a tuned shell-command for Oneliner SYNOPSIS
el [-acdfhioxV] [--command cmd] [--args n] [--format fmt] [--execute] [--inpipe buf] [--outpipe buf] [--serial] [--help] [--ver- sion] [--debug] DESCRIPTION
This manual page documents briefly the el command. This manual page was written for the Debian distribution because the original program does not have a manual page. Instead, it has docu- mentation in the GNU Info format; see below. el is a program that You can make a tuned shell-command for Oneliner with. This command can send S-exp to Emacs, and this can make Emacs evaluate it, too. In other words, you can execute Emacs's function from shell-commands. And you can make Emacs work in closer cooperation with your favorite shell-commands. OPTIONS
These programs follow the usual GNU command line syntax, with long options starting with two dashes (`-'). A summary of options is included below. For a complete description, see the Info files. -c cmd --command cmd cmd means a function of Elisp. Command line arguments and data from stdin means arguments of cmd. If you specify the only -c switch, el output one S-exp by one line of stdin. --a n --args n Specifies the maximum number of arguments of a Elisp function with -c switch. -f fmt --format fmt Allows you to use format string of printf for making a S-exp. -x --execute Requests to Oneliner to evaluate the S-exp to use *Oneliner auto-eval* buffer. -i buf --inpipe buf Gets input from pipe-buffer. You can specify a number of pipe-buffer, too. -o buf --outpipe buf Puts output to pipe-buffer. You can specify a number of pipe-buffer, too. -s --serial Makes el to serialize multiple lines to one line. -h --help Display help message. -V --version Display version identifiers. -d --debug Enable debugging state. SEE ALSO
The programs are documented fully by Oneliner(Shell-mode hooks for Oneliners) available via the Info system. AUTHOR
This manual page was written by OHURA Makoto <ohura@debian.org> for the Debian system (but may be used by others). Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License, Version 2 any later version published by the Free Software Foundation. On Debian systems, the complete text of the GNU General Public License can be found in /usr/share/common-licenses/GPL. el(1)
All times are GMT -4. The time now is 07:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy