I'd like to write a code which prints the default thread's priority, but I don't know if this is possible. So far I created a thread with default attributes, but I didn't find any statement which allows me to store and print its default priority.
// main.c
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <sched.h>
#include "task.h"
int main()
{
pthread_attr_t attr;
struct sched_param prio;
pthread_t tid;
int create = 1;
// default attributes
pthread_attr_init(&attr);
create = pthread_create(&tid, &attr, task, NULL);
if (create != 0) exit(EXIT_FAILURE);
pthread_join(tid, NULL);
return(0);
}
// task.h
#ifndef TASK_H
#define TASK_H
void *task();
#endif
// task.c
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include "task.h"
void *task()
{
printf("I am a simple thread.\n");
pthread_exit(NULL);
}
I didn't find any statement which allows me to store and print its default priority.
It's pthread_attr_getschedparam and sched_param has scheduling priority (at least).
struct sched_param prio;
pthread_attr_getschedparam(&attr, &prio);
printf("sched_priority = %d\n", prio.sched_priority);
Related
I'm trying to write multithread program to calculate usage of processor. The problem is that i don't know how to safely end threads. I have to end them by signal SIGTERM and I tried
while(flag), phread_exit(), exit(), return(void*)0 but non of them work. I need to end infinite loop and return to main function. I've got two results: Exiting the whole program or program stopped and do nothing.
How can I solve it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/sysinfo.h>
#include <ctype.h>
#include <malloc.h>
#include <time.h>
#include <semaphore.h>
#include "cpudata.h"
#include"cpu_result.h"
#include "reader.h"
#include "analyzer.h"
#include "writer.h"
#define NUM_THREADS 3 //Number of threads
//Declaring variables as volatile
volatile struct cpu_data *datax1;
volatile struct cpu_data *datax2;
volatile struct cpu_result *result;
//Declaring semaphores
sem_t sem1;
sem_t sem2;
sem_t sem3;
volatile int flag=1;
volatile int val[NUM_THREADS]={0,0,0};
void handle_sigterm(int signum)
{
flag=0;
}
void *reader(void *ptr) //Thread to read
{ while(flag){
sem_post(&sem3);
sem_wait(&sem1);
sleep(1); //Make sure that semaphores changes value
load_data(datax1);
sleep(1); //Making sure that values datax1 and datax2 are different
load_data(datax2);
if(datax1==datax2) //checking if values are different
{
perror("Datas are the same \n");
}
}
pthread_exit(val[0]);
}
void *analyzer(void *ptr)//Thread to calculate data
{ while(flag){
sem_post(&sem1);
sem_wait(&sem2);
sleep(1);//Make sure that semaphores changes value
for(int i=0;i<NOPT+1;i++)
{
result[i]=calculate(datax1[i],datax2[i]);//calculating data
}
}
pthread_exit(val[1]);
}
void *writer(void *ptr)//thread to write data
{ while(flag){
sem_post(&sem2);
sem_wait(&sem3);
sleep(1);//Make sure that semaphores changes value
writeresult(result);
printf("\n\n\n\n\n\n");
}
pthread_exit(val[2]);
}
int main(int argc, char **argv)
{
struct sigaction action;
memset(&action,0,sizeof(struct sigaction));
action.sa_handler=handle_sigterm;
sigaction(SIGTERM,&action,NULL);
//Allocating memory for data
datax1=(struct cpu_data *)malloc((NOPT+1)*sizeof(struct cpu_data));
datax2=(struct cpu_data *)malloc((NOPT+1)*sizeof(struct cpu_data));
result=(struct cpu_result *)malloc((NOPT+1)*sizeof(struct cpu_result));
pthread_t thread[NUM_THREADS];//declaring threads
//Initializing semaphores
sem_init(&sem1,0,1);
sem_init(&sem2,0,1);
sem_init(&sem3,0,1);
//creating threads
pthread_create(&thread[0],NULL, &reader, NULL);
pthread_create(&thread[1],NULL, &analyzer, NULL);
pthread_create(&thread[2],NULL, &writer, NULL);
//make sure threads are working
for(int i=0;i<NUM_THREADS;i++){
pthread_join(thread[0],NULL);
pthread_join(thread[1],NULL);
pthread_join(thread[2],NULL);
}
//stopping threads
for(int i=0;i<NUM_THREADS;i++){
pthread_cancel(thread[0]);
pthread_cancel(thread[1]);
pthread_cancel(thread[2]);
}
//freeing memory
sem_destroy(&sem1);
sem_destroy(&sem2);
sem_destroy(&sem3);
free(datax1);
free(datax2);
free(result);
printf("SAFETY CLOSING");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *t1(){
long tid=9;
printf("thread ID %ld",tid);
}
void *t2(){
long tid=10;
printf("thread ID %ld",tid);
}
int main()
{
pthread_t thread1,thread2;
int rc;
int i=0;
rc =pthread_create(&thread1,NULL,t1,NULL);
if(rc)
{
printf("ERROR");
exit();
}
pthread_create(&thread2,NULL,t2,NULL);
return 0;
}
This is where I am at, I have created the threads and executed it but I don't know how to call those functions based on a timer.
I am trying to use pthread. In my code below I have defined two global variables (arg1 and arg2) and in the main function I start filling each element of arg2 with 1. I want the pthread to print the 101-th element of arg2 as soon as it is filled by main. But the pthread prints nothing. Actually the changes of arg1 is not followed by pthread and the pthread assumes that arg1 is 0. How can I activate a pthread so that when I write in a buffer in main, the pthread starts reading from buffer simultaneously?
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <math.h>
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <pthread.h>
struct arg_struct {
int arg1;
int arg2[1000];
};
//volatile int arg1;
//int arg2[1000];
void *ProcessThread (void *arguments){
struct arg_struct *args = arguments;
if( args -> arg1==100) {
printf("%d", args -> arg2[ args -> arg1]);
}
pthread_exit(NULL);
return NULL;
}
void main(){
struct arg_struct args;
pthread_t thread1;
void *thread_status;
pthread_create(&thread1, NULL, ProcessThread, (void *)&args);
for ( args.arg1=0; args.arg1<1000; args.arg1++){
args.arg2[args.arg1] =1;
}
pthread_join(thread1,&thread_status);
}
The thread is terminated by the time you get to the for loop. arg1 is still not initialized in ProcessThread. You can use a while loop with the sleep function to test periodically the condition.
I've use pthread for multithreded program and I've got the following situation.
When I run the code without sleep command it causes error at run time, and when I add the sleep command program runs as expected.
With sleep:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t m_writer = PTHREAD_MUTEX_INITIALIZER;
void *print_str(void *args) {
sleep(12);
char *str = (char*) args;
pthread_mutex_lock(&m_writer);
printf("%s", str);
pthread_mutex_unlock(&m_writer);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t t1;
pthread_create(&t1, NULL, print_str, "Hello\n");
pthread_mutex_lock(&m_writer);
printf("LOL\n");
pthread_mutex_unlock(&m_writer);
pthread_join(t1, NULL);
return 0;
}
Without sleep:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t m_writer = PTHREAD_MUTEX_INITIALIZER;
void *print_str(void *args) {
char *str = (char*) args;
pthread_mutex_lock(&m_writer);
printf("%s", str);
pthread_mutex_unlock(&m_writer);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t t1;
pthread_create(&t1, NULL, print_str, "Hello\n");
pthread_mutex_lock(&m_writer);
printf("LOL\n");
pthread_mutex_unlock(&m_writer);
pthread_join(t1, NULL);
return 0;
}
Error:
futex(0x559c3d3df0a0, FUTEX_WAIT_PRIVATE, 2, NULLHello
) = -1 EAGAIN (Resource temporarily unavailable)
strace shows results of system calls. Pthread functions in Linux are not system calls, they are libc functions implemented on top of (potentilally non-trivial) system calls. You are not interested in what internal system calls return. A single successfull call to ptread_mutex_lock may and sometimes does entail several failing syscalls.
In particular, pthread_mutex_lock cannot possibly result in EAGAIN in this program because there are no attempts to lock the mutex recursively, and the default mutex is not recursive in Linux anyway. The FUTEX_WAIT_PRIVATE syscall that pthread_mutex_lock uses internally can and will result in EAGAIN. This is of no interest whatsoever to the application programmer.
I was trying POSIX timers togheter with POSIX signals handling.
When I try to excecute the code you can find downhere, I get:
Errore timer_settime: Invalid argument
On GAPIL book, that is based upon Advanced Linux Programming and Unix network programming, I read that this can happen when inside new_value.value you specified a negative time value or a number of nanoseconds higher than 999999999.
But I think that parameters I have used are okay...
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/fcntl.h>
#include <sys/wait.h>
#include <stdbool.h>
void termination_handler(int signum)
{
printf("Timer scaduto\n");
}
int main()
{
timer_t timer1;
struct sigevent sigeventStruct;
sigeventStruct.sigev_notify = SIGEV_SIGNAL;
sigeventStruct.sigev_signo = 10;
if(timer_create(_POSIX_MONOTONIC_CLOCK, &sigeventStruct, &timer1) == -1)
{
printf( "Errore timer_create: %s\n", strerror( errno ) );
}
printf("timer_create eseguito\n");
struct itimerspec tempoIniziale;
tempoIniziale.it_value.tv_nsec = 0;
struct itimerspec tempoFinale;
tempoFinale.it_value.tv_nsec = 10000000;
if(timer_settime(timer1, 0, &tempoIniziale, &tempoFinale) == -1)
{
printf( "Errore timer_settime: %s\n", strerror( errno ) );
}
struct sigaction newSigAzione, oldSigAzione;
newSigAzione.sa_handler = termination_handler;
//oldSigAzione.sa_handler = termination_handler;
sigemptyset (&newSigAzione.sa_mask);
newSigAzione.sa_flags = 0;
sigaction (SIGEV_SIGNAL, NULL, &oldSigAzione);
if(oldSigAzione.sa_handler != SIG_IGN)
{
//sigaction (SIGEV_SIGNAL, newSigAzione, NULL);
}
/*sigaction (SIGINT, NULL, &oldSigAzione);
if (oldSigAzione.sa_handler != SIG_IGN)
sigaction (SIGINT, &newSigAzione, NULL);
sigaction (SIGHUP, NULL, &oldSigAzione);
if (oldSigAzione.sa_handler != SIG_IGN)
sigaction (SIGHUP, &newSigAzione, NULL);
sigaction (SIGTERM, NULL, &oldSigAzione);
if (oldSigAzione.sa_handler != SIG_IGN)
sigaction (SIGTERM, &newSigAzione, NULL);*/
/*sigaction (SIGTERM, &newSigAzione, NULL);*/
return 0;
}
_POSIX_MONOTONIC_CLOCK is a feature test macro that tells you whether monotonic clocks are available on the system.
The available clock ids you can pass to timer_create() on Linux are:
CLOCK_REALTIME
System-wide realtime clock. Setting this clock requires appropriate privileges.
CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point.
CLOCK_PROCESS_CPUTIME_ID
High-resolution per-process timer from the CPU.
CLOCK_THREAD_CPUTIME_ID
Thread-specific CPU-time clock.
You must also initialize all the members in struct sigevent and struct itimerspec.
E.g. you don't set .tv_sec in the structitimer_spec, only .tv_nsec , which results in garbage values in those members.
...
memset(&sigeventStruct, 0, sizeof sigeventStruct);
...
and
struct itimerspec tempoFinale;
memset(&tempoFinale, 0, sizeof tempoFinale);
tempoFinale.it_value.tv_nsec = 10000000;