linux pthread线程相关,只运行唯一进程相关

#include <QCoreApplication>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<dirent.h>
using namespace std;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int count_value = 0;

void * MyFunc(void * arg)
{
    if(arg == NULL)
    {
        printf("param is not allow NULL.\n");
        return NULL;
    }
    //加锁--所有线程都能访问的全局变量加锁
    pthread_mutex_lock(&mutex);
    int *pnumx = (int*) arg;
    int i = 0;
    while(i < 1000)
    {
        printf("thread%d count=%d\n", *pnumx, count_value++);
        sleep(1);
        i++;
    }
    pthread_mutex_unlock(&mutex);
    return NULL;
}

void * MyFunc2(void * arg)
{
    if (arg == NULL)
    {
        printf("param is not allow NULL!\n");
        return NULL;
    }
    //这个锁和myfunc中的锁是同一个锁,myfunc被锁,这里也会被锁
    pthread_mutex_lock(&mutex);
    int * pnumx = (int *) arg;
    int i = 0;
    while (i < 1000)
    {
        printf("thread%d count=%d\n", *pnumx, count_value++);
        sleep(1);
        i++;
    }
    //解锁
    pthread_mutex_unlock(&mutex);
    return NULL;
}

//Already running return true.
bool IsSingleProcess ()
{

    int no_os_flag=1;

#ifdef linux

    no_os_flag=0;

    std::cout<<"It is in Linux OS!"<<endl;

#endif

#ifdef _UNIX

    no_os_flag=0;

    std::cout<<"It is in UNIX OS!"<<endl;

#endif

#ifdef __WINDOWS_

    no_os_flag=0;

    std::cout<<"It is in Windows OS!"<<endl;

#endif

#ifdef _WIN32

    no_os_flag=0;

    std::cout<<"It is in WIN32 OS!"<<endl;

#endif

    if(1==no_os_flag){

        std::cout<<"No OS Defined ,I do not know what the os is!"<<endl;

    }


    long pid = 0;
    char full_name[1024] = {0};
    char proc_name[1024] = {0};
    int fd;
    pid = getpid();
    std::cout << "pid = " << pid << " ";
    sprintf(full_name, "/proc/%ld/cmdline", pid);
    if (access(full_name, F_OK) == 0)
    {
        fd = open (full_name, O_RDONLY);
        if (fd == -1)
            return false;
        read (fd, proc_name, 1024);
        close (fd);
    }
    else
        return false;

    char self_proc_name[512] = {0};
    char * p = proc_name;
    int pt = 0;
    while (*p != ' ' && *p != '\0')
    {
        self_proc_name[pt] = *p;
        p++;
        pt++;
    }
    string self_final_name = basename(self_proc_name);
    std::cout << " self_final_name = " << self_final_name << std::endl << flush;
    DIR *dir;
    struct dirent * result;
    dir = opendir ("/proc");
    while ((result = readdir (dir)) != NULL)
    {
        if (! strcmp(result->d_name, ".") || ! strcmp (result->d_name, "..") || ! strcmp (result->d_name, "thread-self")
                || ! strcmp (result->d_name, "self") || atol (result->d_name) == pid)
            continue;
        memset(full_name, 0, sizeof(full_name));
        memset(proc_name, 0, sizeof(proc_name));
        sprintf(full_name, "/proc/%s/cmdline", result->d_name);
        if (access(full_name, F_OK) == 0)
        {
            fd = open (full_name, O_RDONLY);
            if (fd == -1)
                continue;
            read (fd, proc_name, 1024);
            close (fd);
            char *q = proc_name;
            pt = 0;
            memset(self_proc_name, 0, sizeof (self_proc_name));
            while (*q != ' ' && *q != '\0')
            {
                self_proc_name[pt] = *q;
                q++;
                pt++;
            }
            string other_final_name = basename(self_proc_name);
            if (self_final_name == other_final_name)
            {
                cout << "full_name = " << full_name << endl << flush;
                cout << "other_final_name: " << other_final_name << endl;
                return true;
            }
        }
    }
    return false;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //condition compile
    #ifdef _WIN32
        if(true == IsSingleProcess())
        {
            std::cout << "process is running in another place" << endl << flush;
            return 0;
        }
    #else
        if(true == IsSingleProcess())  //Already running return true.
        {
            std::cout << "......"<< endl;
            return 0;
        }
    #endif

    cout<<".............START............."<<endl;

    pthread_t thr1,thr2;
    pthread_attr_t attr;

    pthread_attr_init(&attr);
    //设置进程为可分离状态
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    int aa = 1, bb = 2;

    //创建线程
    if (pthread_create(&thr1, &attr, MyFunc, &aa) != 0)
    {
        printf("create thread is failed ! error message :%s\n",
               strerror(errno));
        return -1;
    }
    //释放进程属性对象
    pthread_attr_destroy(&attr);

    if (pthread_create(&thr2, NULL, MyFunc2, &bb) != 0)
    {
        printf("create thread is failed ! error message :%s\n",
               strerror(errno));
        return -1;
    }
    pthread_join(thr2, NULL);
    printf("main end\n");
    return 0;

    return a.exec();
}

猜你喜欢

转载自www.cnblogs.com/mathyk/p/11589642.html
今日推荐