c - can't understand pthread_join() - c

I can not figure out where I'm wrong, after running the code arrived in the for where it runs the pthread_join() , many pthread_join() return with value 3 instead of 0. Furthermore, printing the value of i is not always consistent and this causes segmentation fault and printing several times of the same position.
Code modified as required in the comments
all the includes are for other parts of the program. Testing only this piece of code creates segmentation fault at error 3 on pthread_join()
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <config.h>
#include <sys/select.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
void *threadF(){
printf("hello\n");
pthread_exit((void*)0);
}
int main(int argc, char *argv[]) {
FILE *fileconf=fopen(argv[2],"r");
if(fileconf==NULL){
fprintf(stderr, "Fopen\n",argv[2]);
return -1;
}
set_conf(fileconf); //parse fileconf and set THREADSINPOOL correctly
pthread_t array[THREADSINPOOL];
int i,err,s=0;
for(i=0;i<THREADSINPOOL;i++){
if((err=pthread_create(&array[i],NULL,&threadF,NULL))!=0){
fprintf(stderr,"thread\n");
exit(errno);
}
}
int tmp;
for(i=0;i<THREADSINPOOL;i++){
tmp=pthread_join(array[i],(void *)&s);
printf("thread: %lu terminated\n tmp: %d\n",array[i],tmp);
}
return 0;
}

The problem is that you are passing the address of an int to a function that expects the address of a void *. On a 64-bit system, there's a good chance that an int is only 32-bits whereas a void * is 64-bits. So pthread_join ends up writing 64-bits into a location that is only big enough for 32-bits. The result is that you overwrite memory that shouldn't being changed, and all sorts of undefined behavior follows.
Here's a way to write the code so that the second argument to pthread_join is actually a pointer to a void *
for (i = 0; i < THREADSINPOOL; i++)
{
void *value;
if (pthread_join(array[i], &value) == 0)
printf("thread %d returned %" PRIiPTR "\n", i, (intptr_t)value);
else
printf("thread %d failed\n", i);
}

Related

how do I fix core dump?

I am getting core dump by this little programm.
#include <dirent.h>
int main(void) {
printf("process n%s",(long)getpid());
exit(0);
}
can you explain me why?
You need to know where your functions come from, what they're returning and how to print the return value.
#include <unistd.h>
#include <stdio.h>
int main(void) {
printf("process n%ld", (long)getpid());
}

POSIX Semaphores in C for using in 2 seperated programs (consumer/producer)

I'm trying to implement the producer/consumer problem in C. I know how to handle it with "fork", but in this case I shall implement two programs. One for producer and one for consumer.
For producer: a semaphore has to be initialized and in a loop (to 100), the semaphore shall increment its value and print it. This already works fine.
For consumer: the semaphore initialized in producer, shall be opened and in a loop (to 10) its value shall be decremented and printed.
When I run the process for consumer: a memory-access error is printed.
I have absolutely no idea, what I'm doing wrong. Thanks for any help!
consumer:
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
int value;
sem_t *mySem = sem_open("sem", O_CREAT|O_EXCL , S_IRUSR|S_IWUSR , 0);
for(int i=0; i < 10; i++) {
sem_wait(mySem);
sem_getvalue(mySem, &value);
printf("The value of the semaphore is %d\n", value);
}
sem_close(mySem);
sem_unlink("sem");
return 0;
}
producer:
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
sem_t sem;
int main() {
sem_init(&sem, 0, 0);
int value;
for(int i=0; i < 100; i++) {
sleep(1);
sem_post(&sem);
sem_getvalue(&sem, &value);
printf("The value of the semaphore is %d\n", value);
}
sem_destroy(&sem);
return 0;
}
Hmm, what do you expect sem_init(&sem, 0, 0); to do? What relationship does that sem have with the consumer?
For two unrelated processes to communicate over anyn IPC, they have to agree on a resource by name. That's true if they share a file. It's also true if they share a semaphore. That's what named semaphores are for.
I modified your programs to use one named semaphore. The producer creates it and owns it exclusively; the consumer errors out if it's not there.
Consumer:
#include <err.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <sys/types.h>
static const char name[] = "sem";
int main() {
int value;
sem_t *sem = sem_open(name, 0, S_IRUSR|S_IWUSR, 0);
if( sem == SEM_FAILED ) {
err(EXIT_FAILURE, "sem_open");
}
for(int i=0; i < 10; i++) {
sem_wait(sem);
sem_getvalue(sem, &value);
printf("The value of the semaphore is %d\n", value);
}
if( -1 == sem_close(sem) ) {
err(EXIT_FAILURE, "sem_close");
}
if( -1 == sem_unlink(name) ) {
err(EXIT_FAILURE, "sem_unlink");
}
return 0;
}
Producer:
#include <err.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static const char name[] = "sem";
int main() {
sem_unlink(name); // ignore error if not extant
int value;
sem_t *sem = sem_open(name, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR, 0);
if( sem == SEM_FAILED ) {
err(EXIT_FAILURE, "sem_open");
}
for(int i=0; i < 100; i++) {
sleep(1);
sem_post(sem);
sem_getvalue(sem, &value);
printf("The value of the semaphore is %d\n", value);
}
sem_destroy(sem);
return 0;
}
I think you'll find they work better now. I recommend you follow my lead, though, and check every return code, and exit on error whenever anything goes wrong. For trial code like this, that's the quickest way to get it running correctly.
Thanks for correction. This works perfectly for my purpose.
With sem_init I supposed to initialize the semaphore with 0 as Start-Value.
Seems like the error was using this instead of a pointer and sem_open in the producer-process.
This was my first experience with named semaphores, so it was not easy to see my error.
Thank you very much

Kill() function between two processes

Can I use the kill() function to send a signal to another process? It should be possible, but kill() fails and I can't understand why.
I have two programs (process_1 and process_2). The first one should set a signal handler to increase a variable, than create a child that uses execve and loads process_2. The other program should send the signal using kill(). I used the shared memory to share process_1's pid through a struct (because I have other variables to share). The code of the first program is as follows:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/wait.h>
#define KEY_SM 1234
static int value_to_change=0;
typedef struct keys{
pid_t pid_process;
}keynote;
static void test_handler(int signo){
if(signo=SIGUSR1){
printf("received SIGUSR1\n");
value_to_change++;
}
}
int main(){
if((signal(SIGUSR1, test_handler))==SIG_ERR) perror("Errore allocazione SIGUSR1");
int flags = S_IRUSR|S_IWUSR|IPC_CREAT;
size_t shm_size = sizeof(keynote);
int shm_id = shmget(KEY_MC, shm_size, flags);
keynote *chv = shmat(shm_id, NULL, 0);
chv->pid_process=getpid();
printf("%d\n",chv->pid_process);
int process_2;
process_2=fork();
if(process_2==0){
char* argv[]={"process_1", "process_2", NULL};
if((execve("process_2", argv, NULL))<0) perror("execve error");
exit(0);
}else if(process_2<0)perror("fork error");
return 0;
}
The second program code is:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/wait.h>
#define KEY_SM 1234
typedef struct keys{
pid_t pid_process;
}keynote;
static void signal_to_send(pid_t process){
int ret=kill(process, SIGUSR1);
printf("%d", ret);
}
}
int main(int argc, char **argv){
int pid_of_process_1;
int flags = S_IRUSR|S_IWUSR|IPC_CREAT;
size_t shm_size = sizeof(keynote);
int shm_id = shmget(KEY_MC, shm_size, flags);
keynote *chv = shmat(shm_id, NULL, 0);
signal_to_send(chv->pid_process);
return 0;
}
process_1 does not wait for process_2. It just exits immediately. At which point process_2 may not even have started. So when process_2 sends the signal, process_1 is likely to not exist anymore.
– kaylum

My semaphore from semaphore.h only working after CTR-Z fg

I have a program that prepares some configurations in one process and after that reads those configurations in the parent process. To sync them I'm using semaphores from semaphore.h library. But it seems that it's waiting forever in sem_wait even after I sem_post. It works after I do ctrl-z and fg though. Why is that? Can anyone tell me what's wrong with my code?
My OS is Lubuntu
Semaphore1.h
#ifndef _DNSS_H_
#define _DNSS_H_
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <sys/types.h>
typedef struct configs
{
int i;
sem_t sem;
} CONFIGS;
void init_config(CONFIGS *_configs);
//initiates the threadpool
int init_thread_pool(CONFIGS *configs);
#endif
Semaphore_1.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <semaphore.h>
#include <pthread.h>
#include "semaphore1.h"
void init_config(CONFIGS *_configs)
{
sem_init(&(_configs->sem),1,0); //Creaates a semaphore that is opened when the configs are read to shared memory
_configs->i=2;
fprintf(stderr, "Result of sem_post:%d\n", sem_post(&(_configs->sem)));
}
Semaphore_2.c
#include"semaphore1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int init_thread_pool( CONFIGS *configs)
{
int aux;
fprintf(stderr, "Value of sem_wait():%d\n", sem_wait(&(configs->sem)));
printf("Threadpool initiated with %d threads!", configs->i);
return 1;
}
Semaphore_main.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include "semaphore1.h"
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int argc, char *argv[])
{
pid_t config_pid; //will hold the configuration process id
int _shmid_configs;
CONFIGS *_configs;
_shmid_configs =shmget( IPC_PRIVATE,
sizeof(CONFIGS), IPC_CREAT|0666); //initializes the shared memory
if( _shmid_configs == -1)
{
perror("Error creating shared memory");
}
_configs=shmat(_shmid_configs, NULL,0); //maps the shared memory created to the processp and the config structure
if( _configs == ( CONFIGS*)-1)
{
perror("Error at shmat");
}
//initialization of the processes
config_pid = fork();
if( config_pid < 0)
{
perror("Failed creating configuration manager process");
}
else if( config_pid == 0)
{
init_config(_configs);
printf("Im config!\n");
return 0;
}
//CODE FOR THE gestor de pedidos
printf("right before the threadpool! Configs has a lmit of %d theads\n", _configs->i);
init_thread_pool(_configs);
printf("im parent and im out\n");
sem_destroy(&_configs->sem);
return 0;
}
Compiled with
gcc -g -pthread Semaphore_2.c Semaphore_main.c Semaphore_1.c -o deb
Output:
./deb
right before the threadpool! Configs has a lmit of 0 theads
Result of sem_post:0
Im config!
^Z
[1]+ Stopped ./deb
fg
./deb
Value of sem_wait():0
Threadpool initiated with 2 threads!im parent and im out
sem_init() should be called before fork().
In your current code it is possible for init_thread_pool(_configs); in the parent thread to be called before init_config(), that is you will wait on uninitialized semaphore. It is undefined behaviour.

usage of lsetxattr and lgetxattr functions

I wrote a simple program to test lsetxattr() and lgetxattr() functions.
I just wanted to add an extended property to this file, and get the value again.
But I can't get the result as expected.
So what's the right way to use these two methods?
Thanks!
#define _GNU_SOURCE
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <time.h>
#include <unistd.h>
int main()
{
char *path = "/tmp/abc.txt";
FILE *file = fopen(path, "w");
int id = 101;
if (lsetxattr(path, "user.id", &id, sizeof(int), 0) < 0)
printf("lsetxattr wrong\n");
int result;
if (lgetxattr(path, "user.id", &result, sizeof(int)) != sizeof(int)) {
printf("lgetxattr wrong\n");
}
printf("%d\n", result);
return 0;
}
This is likely because your /tmp mount does not support extended attributes. Looking at the man page:
ENOTSUP
Extended attributes are not supported by the file system, or are
disabled, errno is set to ENOTSUP.
You can verify this by changing the path to be outside of that mount, such as in the current directory (assuming it's outside of that mount of course):
char *path = "abc.txt";
Assuming your other mounts do support extended attributes of course (this is more likely). If you have to do it on /tmp, then you'll have to look at some manuals to figure out how to enable it on /tmp (tmpfs).
Looks like both lsetxattr() and lgetxatter() return -1 by default:
#include <errno.h>
#include <sys/xattr.h>
ssize_t
lgetxattr (const char *__path, const char *__name,
void *__value, size_t __size)
{
__set_errno (ENOSYS);
return -1;
}
stub_warning (lgetxattr)
#include <stub-tag.h>
I found this on glibc's source code: lgetxattr.c and lsetxattr.c

Resources