I have this code:
#include <sys/types.h> /* pid_t */
#include <sys/wait.h> /* waitpid */
#include <stdio.h> /* printf, perror */
#include <stdlib.h> /* exit */
#include <unistd.h> /* _exit, fork */
#include <string.h>
void rec(n)
{
int l, r;
char *new_args[] = {"./bla1", NULL };
if (n)
{
l = fork();
if (l!=0) //parent
r = fork();
if (l == 0 || r == 0)
{
rec(--n); //return;
}
/* if (l == 0) {
rec(--n); return; }
r = fork();
if (r == 0) {
rec(--n); }*/
}
else //call Sorters
{
printf("Execv!!!!\n");
// if (execv(new_args[0], &new_args[0]) < 0) perror("execvp error!\n");
}
}
int main(int argc, char *argv[]) {
rec(3);
return 0;
}
and i get these results:
vasilis#ubuntu:~/Desktop$ Execv!!!!
Execv!!!!
Execv!!!!
Execv!!!!
Execv!!!!
Execv!!!!
Execv!!!!
<---- HERE is empty and it was like it was waiting for a char
vasilis#ubuntu:~/Desktop$
At the point that I am showing it was like it was waiting for a character. I pressed Enter and then the program terminated. Any ideas why?
Each of your children writes the said string to the console.
As it does so in one write() call, the output remains intact.
But: while your child processes do that "in background", the parent process silently exits and returns to the prompt, which you see in the very first line.
As the childs's outputs are printed after the prompt was already given, it is (of course) not repeated. If you enter ls instead of just pressing enter, you'll see that.
Simply your 8 leaf children all want to write a string to the console. And they are doing it simultaneously. As a result, their symbols, including \n, are mixed. More of it, the main task returns and the shell writes the vasilis#ubuntu:~/Desktop$ to the same console, too. It is very strange that you are seeing SOMETHING sensible at all.
They should put their output to some queue, prettily one after another, and to do the output only when all work is done.
Related
Recently I had an assignment where I had to make a program that takes from command line two different commands, separated with a '+', for example:
ps -lu myUsername + ls -la
The objective of the program was to run any two orders simultaneously, with any number of parameters per order, by using fork() and exec(). This was my solution to this problem:
Note: the original problem was meant to be run on a machine with Solaris 5.10 and c89 or c99 standard (gcc 3.4.3)
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <strings.h>
int main (int argc, char* argv[]) {
char delimiter = '+';
char* auxp; //auxiliar pointer
int i = 1;
int position;
while(i < argc){
if (strcmp("+", argv[i]) == 0) {
argv[i] = NULL;
position = i;
}
i++;
}
if (fork() == 0) {
execvp(argv[1], &argv[1]);
exit(1);
}
if (fork() == 0) {
execvp(argv[position+1], &argv[position+1]);
exit(1);
}
wait(NULL);
wait(NULL);
exit(0);
}
This was enough for the assignment but I wanted to make it work with N arguments instead of only 2. I can't reach a systematic way to find the all the addresses. Any help is appreciated, thanks in advice.
For the general case of N commands, need to keep track of where each command starts and ends, and fork a new child whenever the end of a command is found. That could be at a + separator argument, or at the end of the original argument list.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char* argv[]) {
/*
* Note: argc could be in range 0 to INT_MAX.
* Using unsigned int values to avoid arithmetic overflow.
*/
unsigned int start = 1;
unsigned int end = 1;
unsigned int child_count = 0;
/* Note: argv[argc] is a terminating null pointer. */
while(end <= (unsigned int)argc){
if(end == (unsigned int)argc || strcmp("+", argv[end]) == 0){
/* Reached the terminating null pointer or a command separator. */
argv[end] = NULL;
if(start != end){
/*
* Command is not empty.
* Fork a child process to execute the command.
*/
pid_t child = fork();
if(child > 0){
/* Parent forked child successfully. */
child_count++;
}else if(child == 0){
/* This is the child process. Execute command. */
execvp(argv[start], &argv[start]);
exit(1);
}
}
/* Next command starts after this one. */
start = end + 1;
}
/* Looking for terminating null pointer or command separator. */
end++;
}
/* Wait for the child processes to terminate. */
while(child_count){
wait(NULL);
child_count--;
}
exit(0);
}
Note: the argv[end] = NULL; line could be moved into the if(child == 0){ } block (but before the call to execvp) to leave the parent's original argument list intact.
Just move the forking into the loop:
int main (int argc, char* argv[])
{
char** start = ++argv;
unsigned int n = 0;
for(; *argv; ++argv) // profiting from argv being null terminated, too...
{
if (strcmp("+", *argv) == 0)
{
*argv = NULL;
if (fork() == 0)
{
execvp(*start, start);
exit(1);
}
start = argv + 1;
++n; // but need to count how many times we actually forked!
}
}
while(n--)
{
wait(NULL);
}
exit(0);
}
OK, I modified iterating a bit, too – pointers are just so much nicer (personal oppinion...).
Note: This is untested code, if you find a bug please fix yourself...
My program is a rudimental little shell.
It allow you to run programs in PATH as ls, cd..also with arguments.
To run the program type from terminal "./myshell2" then it starts and you can insert how many commands you want.
It starts a child process, runs execvp,it returns and restarts so you can type a new command.
When typed "Q" or "q" all the entire program should terminates.
The problem is that I don't know how to stop it,the code is below.
My idea is, when typed "Q" or "q", to kill the child process created and send a signal to comunicate its bad termination(of child process).
So the final status(from parent) 'll be not 1 and the function returns.
I commented some parts of the code hoping that it's easier to understand.
It works the problem is that to stop it I need of ctrl C.
I would like to say to child process that he must ends with a non-zero value.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
int main(int argc, char * argv[]) {
while(1)
{
pid_t pid = fork();
if (pid == -1) {
perror("fork error");
exit(EXIT_FAILURE);
}
if (pid == 0) { // child process
printf("type the command to start (and arguments if required) \n"
"Q to quit\n");
char *dest[10]; // allow you to insert
char line[4096];//commands from terminal
if (fgets(line,sizeof(line),stdin)==0) return 1;
int i;
line[strcspn(line, "\n")] = '\0';
char *st = line;
for (i=0; i< 10 && (dest[i]=strsep(&st," "))!=NULL;i++)
continue;//now you typed the command
if ( ( memcmp(dest[0],"Q",1)==0 ) // if Q or q the program
|| (memcmp(dest[0],"q",1)==0) ) //must end
{
printf("got it!\n");
if (kill(getpid(),SIGSEGV)==-1) printf("kill error\n");
//in theory the process should terminates with bad status
// and the value of the variable "status" 'll be not 0
// I think that the problem is in this part of the code
}
if( strcmp(dest[0]," ")!=0 )
{
int res = execvp(dest[0], dest);
}
else
{ int res= execvp(dest[1],dest+1);}
perror("execvp error");
exit(EXIT_FAILURE);
}
int status;
pid_t child = wait(&status);
if (child == -1) {
perror("wait error");
exit(EXIT_FAILURE);
}
if (status==1)
break; //so it can exit from the loop that creates new process
setenv("WAIT","TRUE",0); //dont' worry about
//perror("setenv error\n");
if (memcmp("TRUE",getenv("WAIT"),4) == 0 ) //these 6 lines
printf("WAIT=TRUE\n");
else if(memcmp("FALSE",getenv("WAIT"),4) == 0 )
printf("WAIT=FALSE\n");
printf("end current process (status=%d, child=%d)\n", WEXITSTATUS(status), son);
}
return EXIT_SUCCESS;
}
You're printing out WEXITSTATUS() for all cases, but that isn't right. You need to check if the status returned by wait is an exit status or not using WIFEXITED(). If it's non-zero then the child exited normally. Otherwise, you can use WIFSIGNALED() to see if the child was terminated and you'll get the signal from WTERMSIG()
if(WIFEXITED(status))
{
printf("end current process (status=%d, child=%d)\n", WEXITSTATUS(status), son);
}
else if(WIFSIGNALED(status))
{
printf("end current process (signal=%d, child=%d)\n", WTERMSIG(status), son);
}
You really should have the parent process handle the inputting of the command and leave the child process to run it though.
I have a code as below
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
printf("a\n");
fork();
printf("b\n");
if(fork() == 0) {
printf("c\n");
exit(0);
}
printf("d\n");
return 0;
}
Output :
a
b
d
a
b
c
a
b
d
a
b
c
I don't know why the output duplicated many times.
I don't know why the output duplicated many times
Because printf() is buffered.
When a process calls fork(), the resulting child process obtains a copy of the parent's output buffer.
You can empty this output buffer by placing fflush(stdout) just before each call to fork(). In that case output should be:
a
b
b
d
c
d
c
Note that if the output refered to a terminal, it would be line-buffered by default, i.e.: the buffer would be dumped every time a \n is sent to the output. Not so if you are redirecting the output to a file.
When you call fork() it gets a copy of output buffer of the calling process. Buffering is enabled by default, so you get this behavior.
You can use
fflush(stdout);
before a call to fork(). Or, you can also disable buffering using
setbuf(stdout, NULL);
You can read more about fork here. Let me know if you need any more help.
The answer is already in the comments. You are calling fork() twice. So the solution is to just call it once and save the result in a variable like this int pid = fork(). Also, you should check if the fork-call failed (if it does, it returns a negative value).
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
printf("a\n");
int pid = fork();
if (pid < 0)
{
fprintf(stderr, "Can't fork!");
exit(1);
}
printf("b\n");
if(pid == 0)
{
printf("c\n");
}
else
{
printf("d\n");
}
return 0;
}
I am trying to make a program that takes several files, appends them all into one big file. Each append has to be done by a separate thread.
/*
This program creates appends several files together
*/
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
pthread_t *tids;
void *threadout(void *num);
int main(int argc, char *argv[])
{
int numOfFiles = atoi(argv[2]);
int error;
int index;
sem_t sem;
//Used for output file
int outFile;
//Checking to make sure there is the correct number of arguments
if (argc != 4)
{
printf("%s \n", "Wrong number of arguments, exiting program.");
return 1;
}
//checking to make sure there are at least two files to append
if (numOfFiles < 2)
{
printf("%s \n", "Cannot append 1 file or less.");
return 1;
}
//opening/creating file
outFile = open(argv[3], O_WRONLY | O_CREAT, S_IRUSR);
///****************** Allocate space for thread ids ******************/
tids = (pthread_t *)calloc(numOfFiles, sizeof(pthread_t));
if (tids == NULL)
{
perror("Failed to allocate memory for thread IDs");
return 1;
}
if (sem_init(&sem, 0, 1) == -1)
{
perror("Failed to initialize semaphore");
return 1;
}
/****************** Create threads *********************************/
for (index = 0; index < numOfFiles; index++)
{
if (error = pthread_create(tids + index, NULL, threadout, &index))
{
fprintf(stderr, "Failed to create thread:%s\n", strerror(error));
return 1;
}
}
return 0;
}
void * threadout(void *num)
{
printf("Hello");
return NULL;
}
Near the bottom of the program I do the actual creating of the threads. The first thing the thread should do is hit the "threadout" function. However the only way I can get anything to print is if I say to create a large number of threads. So if I tell my program to create 5000 threads, "Hello" will be printed. Not 5000 times though. If I asked it to create 10 threads nothing is printed. Am I doing something wrong when I invoke "threadout"? Thanks
Returning from main causes your entire program to exit, even if other threads are running.
Your main function exits when all threads are started. If you're starting lots of threads, this leaves enough time for the first ones to print. If you're starting few threads, it returns before the first ones get to print anything.
You might want to use pthread_join (called once per thread) to wait for all threads to terminate.
I have been stuck on this for some time.
Let's say I have a C program like the following. I want to be able to send this program some string and get the control after that.
If I do:
--> cat myfile | myprogram
or
--> echo "0123" | myprogram
or
--> myprogram < myfile
I get the ouput (myfile contains "0123")
30 31 32 33
Using the -n option raises a segfault
--> echo -n mystring | ./test
zsh: done echo -n "0123" |
zsh: segmentation fault ./test
I also tried with a named pipe, but it didn't work either.
I would like to be able to do something like
cat myfile | myprogram
and get back the control so that I can type other characters.
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 int main (int argc, char *argv[]) {
6 int i = 0, j;
7 unsigned char buf[512];
8 unsigned char x;
9
10 while ((x = getchar()) != '\n') {
11 buf[i] = x;
12 i++;
13 }
14
16 for (j = 0; j < i; j++) {
17 printf("%x ", buf[j]);
18 }
19 printf ( "\n" );
20
21 return EXIT_SUCCESS;
22 } // end of function main
EDIT:
Below is the wrapper I have come up with.
It does everything I want, except that the output of the child exec-ed file is not properly displayed.
Without the wrapper:
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2+2
4
With the wrapper:
$ ./wrapper bc
2+2
enter
4
Deleting the line
dup2(pipefd[0], 0); // Set the read end of the pipe as stdin.
makes the child stdout display correctly, but of course breaks the wrapper.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <assert.h>
int main(int argc, char const *argv[]) {
int cpid;
int pipefd[2];
if (pipe(pipefd) == -1) { perror("pipe.\n"); exit(errno); }
cpid = fork();
if (cpid == -1) { perror("fork."); exit(errno); }
if (cpid) {
// Parent --------------------------------------------------------
int buf_size = 8192;
char buf[buf_size];
size_t file;
// Close the unused read end of the pipe.
close(pipefd[0]);
// Leave a bit of time to the child to display its initial input.
sleep(2);
while (1) {
gets(buf);
if (strcmp("enter", buf) == 0) {
write(pipefd[1], "\n", 1);
} else if (-1 != (file = open(buf, O_RDONLY))) {
// Dump the output of the file to the child's stdin.
char c;
while(read(file, &c, 1) != 0) {
switch(c) {
case '\n':
printf("(skipped \\n)");
break;
default:
printf("%c", c);
write(pipefd[1], &c, 1);
};
}
printf("\n");
} else {
// Dump input to the child's stdin, without trailing '\n'.
for (int i = 0; (buf[i] != 0); i++) {
write(pipefd[1], buf + i, 1);
}
}
}
// Wait for the child to exit.
printf("Waiting for child to exit.\n");
wait(NULL);
} else {
// Child ---------------------------------------------------------
// Close the unused write end of the pipe.
close(pipefd[1]);
// Set the read end of the pipe as stdin.
dup2(pipefd[0], 0); // Set the read end of the pipe as stdin.
char** program_arguments = (char**)(argv + 1);
if (execvp(argv[1], program_arguments) < 0) {
perror("execvp.\n");
exit(errno);
}
}
}
I do not think it is possible to achieve this using named pipes if you can not modify the behavior of the program. Since in essence named pipes are no different then giving the output from standard input with redirection.
I also do not think it is possible if you use pipe or redirection properties of the shell, since always an EOF is sent to your program in this case and you can not ignore EOF since you can not modify the program.
A possible solution is to use a wrapper. The wrapper will first read the prepared input, send them to your program, after the prepared input finishes the wrapper switches to standard input. Actual program just keeps consuming input, it is not aware of the actual source of the data.
Only drawback is, you can not provide prepared input with pipes or redirection, you have to supply a filename. (I'm not sure a named pipe will work or not.) The reason is obvious, if you provide the prepared input to wrapper from standard input then the same problem exists for wrapper. By this way you are just delegating the problem to wrapper, which you can design any way you want.
A possible implementation in C (modified from a similar wrapper I've used, not tested extensively):
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
int main(int argc, char * argv[]) {
char c;
char **pargs ;
char buf[20];
int n;
int pipe_fd[2];
int pid;
pargs = argv+2;
if (pipe(pipe_fd) < 0) {
perror("pipe failed");
exit(errno);
}
if ((pid=fork()) < 0) {
perror ("Fork failed");
exit(errno);
}
if (! pid) {
close(pipe_fd[1]);
dup2(pipe_fd[0],0);
close(pipe_fd[0]);
if (execvp(argv[2],pargs) < 0) {
perror("Exec failed");
exit(errno);
}
} else {
size_t filedesc = open(argv[1],O_RDONLY);
while((n = read(filedesc, buf, 100)) > 0)
write (pipe_fd[1], buf, n);
while((n = read(0, buf, 100)) > 0)
write (pipe_fd[1], buf, n);
}
}
You can run your program with this wrapper as :
./wrapper input.txt myprog possible command line arguments
You can put your initial input into input.txt.
A simpler solution is to reopen the standard input. However if you simply try to open it as if you are opening a file, it does not work. You should open the terminal stream and copy it to standard input of your application. You can do it (again by using a wrapper) with something like:
size_t tty = open("/dev/tty",O_RDONLY);
dup2(tty,0);
Not to mention this second solution is for Linux and not portable.
In this example I use tail -f, not your C program
mkfifo /tmp/pipe # Open Pipe
tail -f /tmp/pipe & # Start your program and put it into the background
Now you also can send data to your program that runs in the background
echo "foobar" > /tmp/pipe
I hope this helps?
You could modify your program to accept 1 null character then continue on...it might work:
replace line 10 with something like
while (TRUE)
{
x = getchar();
if (x == '\n')
break;
if (x == '\0')
{
if (seen)
break;
seen = TRUE;
}
...