For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > July 2007 > some strange output..









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author some strange output..
Ravi

2007-07-20, 7:10 pm

/* Create a thread it prints o's continuously. The main thread prints
x's continuously. */
#include <stdio.h>
#include <pthread.h>
void *print_xs(void *v)
{
while(1) {
fprintf(stderr,"x");
}
}

int main(void)
{
pthread_t tid;
pthread_create(&tid,NULL,&print_xs,NULL);
while(1)
fprintf(stderr,"o");
return 0;
}

The given program when executed with command line as `a.out &2>1'
executes catastrophically. Ctrl-C goves the bash prompt but the
program continues to run. Why?

Lew Pitcher

2007-07-20, 7:10 pm

On Jul 20, 11:29 am, Ravi <ra.ravi....@gmail.com> wrote:
[snip]
> The given program when executed with command line as `a.out &2>1'
> executes catastrophically. Ctrl-C goves the bash prompt but the
> program continues to run. Why?


Because you messed up the commandline. What you entered was
interpreted as
a.out &
2>1
which is two shell commands.

The first shell command
a.out &
runs a.out as a background process, detached from the shell. With your
code, that process won't stop unless you explicitly kill it.

The second shell command (a I/O redirect)
2>1
creates the file named "1" (as a side effect), and does very little
else

So, you are left with
a) a file called "1" in the cwd, and
b) a process running in background.

What you probably /meant/ to do was
a.out 2>&1
which runs a.out (suspending the current shell), and redirects all
stderr output from a.out to the current stdout

HTH
--
Lew



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com