Question: pthread_cancel() and printf()


 
Thread Tools Search this Thread
Top Forums Programming Question: pthread_cancel() and printf()
# 1  
Old 10-07-2005
Question Question: pthread_cancel() and printf()

Hello!
First of all, sorry for my English, I'm not a native English speaker.

I know, that printf() function uses write() function. "man cancellation" says that write() function is a cancellation point. But when I call pthread_cancel() for my thread, which calls printf() in infinite cycle, it doesn't cancel! It means there are no cancellation points in printf(). But how is it possible? write() is a cancellation point, printf() uses write() and there are no cancellation points in printf()... I'm confused...

Could anybody please explain to me what the matter is?

P.S. The system is Solaris 10.
# 2  
Old 10-07-2005
The write() call for printf is deferred - meaning it doesn't happen until the buffer printf is using becomes full or is flushed.

But what you are saying means that the thread is not cancellable. Not that printf does not call write().

See man pthread_setcancelstate()
# 3  
Old 10-08-2005
Here's my program (lite version Smilie ):

Code:
#include <stdio.h>
#include <pthread.h>

#define STR	"Windows must die\n"
#define TIMEOUT	2

void* start(void* p) {
  int i = 0;  

  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);	  // line_D
	
  for (i = 0;; ++i) {
    //write(1, STR, strlen(STR)); // line_A
    //pthread_testcancel();   // line_B   
    printf("%s", STR);   // line_C
  }
	
  return NULL;
}

int main() {
  int loc = 0;
  pthread_t thread;

  setvbuf(stdout, NULL, _IONBF, 0);
  pthread_create(&thread, NULL, start, NULL);
  sleep(TIMEOUT);
  pthread_cancel(thread);
  pthread_exit(0);
  return 0;
}

If I comment line_C and line_B and uncomment (I'm not sure that this word is exist, but I hope you forgive me, if I'm wrong Smilie ) line_A, it works fine (terminates in 2 seconds). And I understand why it does.
If I comment line_A and uncomment line_B and line_C, it works fine too.
But I can't understand, why it doesn't work when I comment line_A and line_B and uncomment line_C!
I added line_D after your reply, but it doesn't change anything.
Maybe, I have to specify some compilation keys? I compile the program by using "gcc prog_name.c" command.
# 4  
Old 10-10-2005
try compiling like so :
gcc prog_name.c -lpthread
# 5  
Old 10-13-2005
Doesn't work too. In modern versions all the lpthread and lthread functions are included into libc, and lpthread and lthread are only the filters for libc. So it isn't necessary to compile programs with -lpthread.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printf question: getting padded zero in decimal plus floating point together.

Hi Experts, Quick question: I am trying to get the output with decimal and floating point but not working: echo "20.03" | awk '{printf "%03d.2f\n" , $0 }' 020.2f How to get the output as : 020.03 Thank you. (4 Replies)
Discussion started by: rveri
4 Replies

2. Programming

pthread_cancel failure

I'm running a simple web server and seem to be having a problem canceling sessions. When a new request is received I start a thread to handle that session's requests. Since I want to keep the pipe open for a long time - 10 minutes or maybe 2 hours - I also have a session manager that... (4 Replies)
Discussion started by: John S.
4 Replies

3. Shell Programming and Scripting

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (2 Replies)
Discussion started by: kbmkris
2 Replies

4. UNIX for Dummies Questions & Answers

AWK printf command question

Hi, I am using below awk code to convert a csv file data into fixed file format. awk 'BEGIN { FS = "," fmt = "%10s%010d%10s%d%1d\n" } NR>1 { printf fmt, $1, $2, $3, $4*100, $5 }' /data/mydata.csv > /data/fixed.dat Data in mydata.csv ================... (1 Reply)
Discussion started by: kbmkris
1 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. UNIX for Advanced & Expert Users

Memory leak while using pthread_cancel()

I tried to execute a sample pthread program to cancel a newly created one using pthread_cancel(). but using valgrind on my code shows some memory leak. My Code: #include "iostream" #include "unistd.h" #include "pthread.h" #include "signal.h" using namespace std; void handler(int); void*... (4 Replies)
Discussion started by: kcr
4 Replies

7. UNIX for Dummies Questions & Answers

AwK printf question

Hi, Does anyone know a easy way to printf $3,$4, ... all the way to the last field in the file? I will need to modify $1 and $2 and then printf modified $1 and $2 and the rest of the fields(which are not changed). I know I can use NF as the total number of field. Do I use a for next statement to... (4 Replies)
Discussion started by: whatisthis
4 Replies

8. 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

9. 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

10. Programming

Use of pthread_cancel()

Hi all, I am working on a piece of multi threaded code, where I have a main thread looping in increments of 5ms, while it is waiting on a helper thread which is writing to a remote location. This helper thread keeps updating the main thread about progress of the write operation and the moment we... (1 Reply)
Discussion started by: ravneetd
1 Replies
Login or Register to Ask a Question