You are not logged in.

#1 2023-10-22 09:57:51

Greatpi
Member
Registered: 2022-08-27
Posts: 10

What's the different between "kill" and exit when terminate a process?

When I use shared memory to accomplish synchronizes between just two processes. I find when  I kill a process use command "kill  $(pidof [process name])", another one will stuck at function "pthread_cond_broadcast" (or pthread_cond_signal)with high probability. Why?

That is A process, broadcast.c:

#include <pthread.h>
#include <time.h>
#include <stdlib.h>

#include <stdio.h>
#include <errno.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#define SHM_FILE    "shm_file"

typedef struct 
{
    int count;

    pthread_cond_t cond;
    pthread_mutex_t mutex;
}Shared;

Shared* creat_mem()
{
    Shared *ptr;
    int fd;

    shm_unlink(SHM_FILE);
    fd = shm_open(SHM_FILE, O_RDWR | O_CREAT | O_EXCL, 0777);
    fchmod(fd, 0777);
    ftruncate(fd, sizeof(Shared));
    ptr = mmap(NULL, sizeof(Shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    close(fd);

    return ptr;
}

//生产者线程
void* p_func(void *parm)
{
    Shared *ptr = (Shared*)parm;
    while(1)
    {
        // pthread_mutex_lock(&ptr->mutex);
	ptr->count++;
        // pthread_mutex_unlock(&ptr->mutex);
	//通知等待条件变量变为真的消费者
        printf("0000000 \n");
	pthread_cond_broadcast(&ptr->cond);
	// pthread_cond_signal(&ptr->cond);
        printf("111111111 \n");
	//休眠1~4秒
	sleep(2);
    }
    return NULL;
}

int main(void)
{
    Shared *ptr = NULL;

    printf("broadcast start \n");
    //创建共享内存
    ptr = creat_mem();

    //初始化锁,初始化条件变量
    pthread_mutex_init(&ptr->mutex, NULL);
    
    pthread_condattr_t cAttr;
    pthread_condattr_init(&cAttr);
    pthread_condattr_setpshared(&cAttr, PTHREAD_PROCESS_SHARED);
    pthread_cond_init(&ptr->cond, &cAttr);

    //创建生产者线程。
    pthread_t tid;
    pthread_create(&tid, NULL, p_func, ptr);
    //阻塞等待线程汇合
    pthread_join(tid, NULL);

    printf("broadcast end \n");

    shm_unlink(SHM_FILE);
    return 0;
}

Complile command:

gcc -g broadcast.c -o broadcast -lpthread -lrt

And that is B process, wait.c:

#include <pthread.h>
#include <time.h>
#include <stdlib.h>

#include <stdio.h>
#include <errno.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

#define SHM_FILE    "shm_file"

typedef struct 
{
    int count;

    pthread_cond_t cond;
    pthread_mutex_t mutex;
}Shared;

Shared * creat_open()
{
    Shared *ptr;
    int fd;
    struct stat buf;

    fd = shm_open(SHM_FILE, O_RDWR, 0777);
    fstat(fd, &buf);
    //ftruncate(fd, sizeof(Shared));
    ptr = mmap(NULL, sizeof(Shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    close(fd);

    return ptr;
}


//消费者线程
void* c_func(void *parm)
{
    Shared *ptr = (Shared*)parm;
    struct timespec stAbsTime = {0};
    
	while(1)
	{
		//加锁
		pthread_mutex_lock(&ptr->mutex);
		printf("wait data!!! \n");
		pthread_cond_wait(&ptr->cond, &ptr->mutex);
		//解锁
		pthread_mutex_unlock(&ptr->mutex);
		//消费摘取的节点
		printf("ptr->count:%d\n", ptr->count);
		//休眠1~4秒
		//_exit(-1);
		sleep(1);
	}
	return NULL;
}

int main(void)
{
        Shared *ptr = NULL;

        //打开共享内存
        ptr = creat_open();
	//创建消费者线程
	pthread_t cid;
	pthread_create(&cid, NULL, c_func, ptr);
	
	//阻塞等待线程汇合
	pthread_join(cid, NULL);
	return 0;
}

Complile command:

gcc -g wait.c -o wait -lpthread -lrt

Run A process before B process, because A process create the shared memory.
The question is when Run A process and B process, then kill B process with command "kill  $(pidof wait)" and run B process again, A process will stuck at function "pthread_cond_broadcast" (or pthread_cond_signal) with high probability.
But When  B process terminate itself use "_exit(-1);" in code, there is OK; Why?

Last edited by Greatpi (2023-10-22 10:05:04)

Offline

#2 2023-10-22 14:17:19

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,422

Re: What's the different between "kill" and exit when terminate a process?

http://s3.media.squarespace.com/product … 970c-800wi

Install a signal handler, intercept sigterm and use that to terminate the thread (or set a flag to the thread to eg. clean up and then exit)
See also eg. https://stackoverflow.com/questions/208 … ad-library

Offline

#3 2023-10-22 15:17:55

Greatpi
Member
Registered: 2022-08-27
Posts: 10

Re: What's the different between "kill" and exit when terminate a process?

seth wrote:

http://s3.media.squarespace.com/product … 970c-800wi

Install a signal handler, intercept sigterm and use that to terminate the thread (or set a flag to the thread to eg. clean up and then exit)
See also eg. https://stackoverflow.com/questions/208 … ad-library

it does not work. In the B process (wait.c) ,I add a function in main function:

void fun(int sig)
{
    printf("receive:%d", sig);
    exit(0);
}
int main()
{
   signal(15, fun);
   //........
}

There is no change when I kill B process.

Offline

#4 2023-10-22 15:30:06

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,422

Re: What's the different between "kill" and exit when terminate a process?

Assuming that the signal handler is executed (you get the printf): In what thread is that exit statement executed?
Where was it executed before?
What did I actually suggest and what are the approaches in the linked SO thread?

Offline

Board footer

Powered by FluxBB