Sponsored Content
Top Forums Programming Question: pthread_cancel() and printf() Post 85848 by jim mcnamara on Friday 7th of October 2005 12:21:46 PM
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()
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
PTHREAD_CANCEL(3)					     Linux Programmer's Manual						 PTHREAD_CANCEL(3)

NAME
pthread_cancel - send a cancellation request to a thread SYNOPSIS
#include <pthread.h> int pthread_cancel(pthread_t thread); Compile and link with -pthread. DESCRIPTION
The pthread_cancel() function sends a cancellation request to the thread thread. Whether and when the target thread reacts to the cancel- lation request depends on two attributes that are under the control of that thread: its cancelability state and type. A thread's cancelability state, determined by pthread_setcancelstate(3), can be enabled (the default for new threads) or disabled. If a thread has disabled cancellation, then a cancellation request remains queued until the thread enables cancellation. If a thread has enabled cancellation, then its cancelability type determines when cancellation occurs. A thread's cancellation type, determined by pthread_setcanceltype(3), may be either asynchronous or deferred (the default for new threads). Asynchronous cancelability means that the thread can be canceled at any time (usually immediately, but the system does not guarantee this). Deferred cancelability means that cancellation will be delayed until the thread next calls a function that is a cancellation point. A list of functions that are or may be cancellation points is provided in pthreads(7). When a cancellation requested is acted on, the following steps occur for thread (in this order): 1. Cancellation clean-up handlers are popped (in the reverse of the order in which they were pushed) and called. (See pthread_cleanup_push(3).) 2. Thread-specific data destructors are called, in an unspecified order. (See pthread_key_create(3).) 3. The thread is terminated. (See pthread_exit(3).) The above steps happen asynchronously with respect to the pthread_cancel() call; the return status of pthread_cancel() merely informs the caller whether the cancellation request was successfully queued. After a canceled thread has terminated, a join with that thread using pthread_join(3) obtains PTHREAD_CANCELED as the thread's exit status. (Joining with a thread is the only way to know that cancellation has completed.) RETURN VALUE
On success, pthread_cancel() returns 0; on error, it returns a nonzero error number. ERRORS
ESRCH No thread with the ID thread could be found. CONFORMING TO
POSIX.1-2001. NOTES
On Linux, cancellation is implemented using signals. Under the NPTL threading implementation, the first real-time signal (i.e., signal 32) is used for this purpose. On LinuxThreads, the second real-time signal is used, if real-time signals are available, otherwise SIGUSR2 is used. EXAMPLE
The program below creates a thread and then cancels it. The main thread joins with the canceled thread to check that its exit status was PTHREAD_CANCELED. The following shell session shows what happens when we run the program: $ ./a.out thread_func(): started; cancellation disabled main(): sending cancellation request thread_func(): about to enable cancellation main(): thread was canceled Program source #include <pthread.h> #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <unistd.h> #define handle_error_en(en, msg) do { errno = en; perror(msg); exit(EXIT_FAILURE); } while(0) static void * thread_func(void *ignored_argument) { int s; /* Disable cancellation for a while, so that we don't immediately react to a cancellation request */ s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); if (s != 0) handle_error_en(s, "pthread_setcancelstate"); printf("thread_func(): started; cancellation disabled "); sleep(5); printf("thread_func(): about to enable cancellation "); s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); if (s != 0) handle_error_en(s, "pthread_setcancelstate"); /* sleep() is a cancellation point */ sleep(1000); /* Should get canceled while we sleep */ /* Should never get here */ printf("thread_func(): not canceled! "); return NULL; } int main(void) { pthread_t thr; void *res; int s; /* Start a thread and then send it a cancellation request */ s = pthread_create(&thr, NULL, &thread_func, NULL); if (s != 0) handle_error_en(s, "pthread_create"); sleep(2); /* Give thread a chance to get started */ printf("main(): sending cancellation request "); s = pthread_cancel(thr); if (s != 0) handle_error_en(s, "pthread_cancel"); /* Join with thread to see what its exit status was */ s = pthread_join(thr, &res); if (s != 0) handle_error_en(s, "pthread_join"); if (res == PTHREAD_CANCELED) printf("main(): thread was canceled "); else printf("main(): thread wasn't canceled (shouldn't happen!) "); exit(EXIT_SUCCESS); } SEE ALSO
pthread_cleanup_push(3), pthread_create(3), pthread_exit(3), pthread_join(3), pthread_key_create(3), pthread_setcancelstate(3), pthread_setcanceltype(3), pthread_testcancel(3), pthreads(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2008-11-17 PTHREAD_CANCEL(3)
All times are GMT -4. The time now is 05:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy