printf when used for different processes


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers printf when used for different processes
# 1  
Old 09-30-2010
printf when used for different processes

I am playing with function fork() (creating few processes)and trying to put some output (with process id,child id and so on) to the shell and sometimes i get output for 2 proccesses mixed up on same line. sometimes I would get something like this...

Code:
myShell$  ./a.out
proccess1  processID......  parentID......childID
proccess2  processID......  parentID......childID
proccess4  processID......  parentID......childID
proccess3  processID......  parentID......childID
myShell$

You see how order of processes is mixed up..

Is this happening because prinff buffers outputs and doesn't send them to screen right away? Does fprintf buffers as well..

Sometimes i would get something like this.....

Code:
myShell$  ./a.out
proccess1  processID......  parentID......childID
proccess2  processID......  parentID......childID
myShell$ proccess3  processID......  parentID......childID
 proccess4  processID......  parentID......childID

Cananyone help me understand what is happening please

Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 10-01-2010 at 01:14 AM..
# 2  
Old 10-01-2010
You are seeing context switching. Process #13 runs printf code. But it only gets part way through the code and the OS decides to give the cpu to process #82. It printf's more stuff.

That way your output gets jumbled - part from one process, part from others.

It gets technical if you want to prevent that. You need to use an I/O operation that does not interrupt midway, a single call - instead of the 100's of lines of code in printf. write() is what you want.
example:
Code:
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main()
{
    char tmp[64]={0x0};
    pid_t pid=getpid();
    size_t len=0;    
    
    sprintf(tmp,"My pid is %d\n", pid);
    len=strlen(tmp);
    write(1, tmp, len);  // 1 is stdout
    return 0;
}

# 3  
Old 10-01-2010
isnt printf....

It looks to me like process 2 printed...Then process 3 was about to print but his time was taken away and given back to the shell(you can see shell prompt in second code example) then time was given back to process 3 which printed and then process 4 printed.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printf %T not working

Dear All, this script was working well enough few days back. Now it started acting up. Could anyone please throw some lights about what may be the reason for it's not working. ts=$( printf "%(%s)T" "now" ) under debug mode this is what I get printf "%(%s)T" "now" ++ printf '%(%s)T'... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

2. Shell Programming and Scripting

awk with printf

Hi, I am using the following code to assign a count value to a variable. But I get nothing. Do you see anything wrong here. I am new to all this. $CTR=`remsh $m -l $MACHINES{$m} -n cat $output | grep -v sent | grep \"$input\" | sort -u | awk '{print $5}'`; Upto sort - u it's... (2 Replies)
Discussion started by: nurani
2 Replies

3. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

4. Shell Programming and Scripting

AWK printf help

Target file contains short text (never more than 1 line) and filenames. The format is, e.g.,: TEXT1 filename1 TEXT2 TEXT3 filename3dddd filename3dddd TEXT4 filename4 TEXT5 filename5dddd filename5dddd filename5 where dddd is a random 4-digit whole number. Desired output: (4 Replies)
Discussion started by: uiop44
4 Replies

5. UNIX for Dummies Questions & Answers

Need help with printf

Hi, I have just completed my first script (:D) and now i just need to format it with printf. This is what I have: #!/bin/ksh TOTB=0 TOTF=0 TOTI=0 HOST=`hostname` echo " FSYSTEM BLKS FREE INUSE MOUNTEDON" df -m | grep -v ":"|grep -v Free|grep -v "/proc"| while read FSYSTEM... (2 Replies)
Discussion started by: compan023
2 Replies

6. Solaris

Identifying and grouping OS processes and APP processes

Hi Is there an easy way to identify and group currently running processes into OS processes and APP processes. Not all applications are installed as packages. Any free tools or scripts to do this? Many thanks. (2 Replies)
Discussion started by: wilsonee
2 Replies

7. Shell Programming and Scripting

printf

How to print output in following format? A..................ok AA................ok AAA..............ok AAAAAA........ok "ok" one under one (4 Replies)
Discussion started by: mirusnet
4 Replies

8. UNIX for Advanced & Expert Users

Monitoring Processes - Killing hung processes

Is there a way to monitor certain processes and if they hang too long to kill them, but certain scripts which are expected to take a long time to let them go? Thank you Richard (4 Replies)
Discussion started by: ukndoit
4 Replies

9. Programming

printf

What is the output of the following program considering an x86 based parameter passing sequence where stack grows towards lower memory addresses and that arguments are evaluated from right to left: int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() {... (2 Replies)
Discussion started by: arunviswanath
2 Replies

10. Shell Programming and Scripting

printf question

can you take input from another command and do printf? such as awk '{print $2,$1}' | sort -k1,1 -k2,2 | printf "%-10s,%15s" this does not work.. but there must be a way.. please help me.. thank you. (3 Replies)
Discussion started by: hankooknara
3 Replies
Login or Register to Ask a Question