- 论坛徽章:
- 0
|
遇到一个诡异的问题,main程序启动后启三个线程,然后main函数while(1)。
通过ps命令查看应该仅仅会出现同名进程4个才对,但是出现了五个?
下面的是程序,使用shell脚本运行起来,因为是demo所以shell极其简单。
有没有兄弟留意过这个情况的?有什么解释没有,感激涕淋!!!- #!/bin/sh
- echo "Start Openplat Demo..."
- /devinfo/package/openplatform_demo/openplatform_demo &
复制代码- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <errno.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <stdbool.h>
- #include <pthread.h>
- #define OPEN_PLAT_FLASH_BASE_PATH "/devinfo"
- #define OPEN_APP_BASE_PATH "/devinfo/package"
- #define OPEN_APP_NAME "openplatform_demo"
- //#define OPEN_PLAT_FLASH_BASE_PATH "/mnt/mmc01"
- //#define OPEN_APP_BASE_PATH "/mnt/mmc01/package"
- #define OPEN_PLATFORM_MSG_IPCKEY 0x010
- #define OPEN_PLATFORM_MSG_PRI_SET 1 // 参数设置接收消息类型
- #define OPEN_PLATFORM_MSG_PRI_SET_RESP 2 // 参数设置应答消息类型
- #define OPEN_PLATFORM_MSG_PRI_GET 3 // 参数获取接收消息类型
- #define OPEN_PLATFORM_MSG_PRI_GET_RESP 4 // 参数获取应答消息类型
- #define OPEN_PLATFORM_MSG_PRI_UPLOAD_LICENSE 5 // license 数据接收消息类型
- #define OPEN_PLATFORM_MSG_PRI_UPLOAD_LICENSE_RESP 6 // license 导入应答消息类型
- #define OK 0
- #define ERROR -1
- #define TRUE 1
- #define FALSE 0
- typedef int BOOL;
- typedef struct
- {
- long mtype;
- char buffer[8*1024];
- }msgbuf_ext;
- BOOL terminated = FALSE;
- /**@param[in] msqid: 消息队列描述符
- *@param[in] buf: 消息队列缓冲区
- *@param[in] cmd: IPC_STAT:该命令用来获取消息队列信息,返回的信息存贮在buf指向的msqid结构中;
- IPC_SET:该命令用来设置消息队列的属性,要设置的属性存储在buf指向的msqid结构中;可设置属性包括:msg_perm.uid、msg_perm.gid、msg_perm.mode以及msg_qbytes,同时,也影响msg_ctime成员。
- IPC_RMID:删除msqid标识的消息队列;
- */
- static int msg_ctl(int msqid, int cmd, struct msqid_ds *buf, char* appname)
- {
- if(msgctl(msqid, cmd, 0) < 0)
- {
- fprintf(stderr, "<%s %s LINE %d>msgctl failed [%s]!!\n", __FILE__, __FUNCTION__, __LINE__, strerror(errno));
- return ERROR;
- }
-
- return OK;
- }
- /*
- *@brief 创建一个消息队列或者获取一个消息队列的描述符
- *@param[in] appname: 第三方应用程序名称,用于构造创建消息队列的path
- *@param[in] b_creat: TRUE 表示创建一个消息队列,FALSE: 表示获取消息队列描述符
- *@return ERROR 错误,成功,消息队列描述符
- */
- static int create_msgq(char* appname, BOOL b_creat)
- {
- char path[128];
- key_t key = -1;
- int msgid;
-
- if(appname == NULL)
- {
- fprintf(stderr, "create_msgq input is null!!!\n");
- return ERROR;
- }
- memset(path, 0, sizeof(path));
- sprintf(path, "%s/%s", OPEN_APP_BASE_PATH, appname);
-
- key = ftok(path, OPEN_PLATFORM_MSG_IPCKEY);
- fprintf(stderr, "create_msgq GET path \"%s\" , key [%d]!\n", path, key);
- // 获取消息队列描述符
- msgid = msgget(key, IPC_CREAT);
-
- if(msgid == -1)
- {
- fprintf(stderr, "create_msgq msgget Failed [%s]!!!!\n", strerror(errno));
- return ERROR;
- }
- return msgid;
- }
- static int send_msg(int msqid, msgbuf_ext *msgp, size_t msgsz, int msgflag, char* appname)
- {
- int ret = 0;
-
- if(msgp == NULL)
- {
- fprintf(stderr, "send_msg input is err!!\n");
- return ERROR;
- }
-
- ret = msgsnd(msqid, msgp, msgsz, msgflag);
- if(ret < 0)
- {
- fprintf(stderr, "send_msg msgsnd failed!!\n");
- return ERROR;
- }
- return OK;
- }
- static int recv_msg(int msqid, msgbuf_ext *msgp, size_t msgsz, long msgtype, int msgflag, char* appname)
- {
- int ret = 0;
-
- if(msgp == NULL)
- {
- fprintf(stderr, "recv_msg input is err!!\n");
- return ERROR;
- }
-
- ret = msgrcv(msqid, msgp, msgsz, msgtype, msgflag);
- if(ret < 0)
- {
- fprintf(stderr, "recv_msg msgsnd failed ret %d\n", ret);
- return ERROR;
- }
- return OK;
- }
- static void signal_handler(int sig, siginfo_t *siginfo, void* opaque)
- {
- (void)opaque;
- fprintf(stderr, "Signal was received: %s. Pid: %d. Uid: %d.\n", strsignal(sig), siginfo->si_pid, siginfo->si_uid);
- if (sig == SIGINT || sig == SIGTERM)
- {
- terminated = true;
- }
- else if(sig == SIGUSR2)
- {
- // 恢复默认值信号
- fprintf(stderr, "@@@@@@simple reset app param!!!!\n");
- }
- }
- static void signal_handler_init()
- {
- struct sigaction sigact;
- memset(&sigact, 0, sizeof(struct sigaction));
- sigact.sa_flags = SA_SIGINFO;
- sigact.sa_sigaction = &signal_handler;
- sigemptyset(&sigact.sa_mask);
- sigaddset(&sigact.sa_mask, SIGINT);
- sigaddset(&sigact.sa_mask, SIGTERM);
- sigaddset(&sigact.sa_mask, SIGABRT);
-
- if (sigprocmask(SIG_UNBLOCK, &sigact.sa_mask, NULL) != 0)
- {
- fprintf(stderr, "Sigprocmask failed. Error: %s\n", strerror(errno));
- exit(EXIT_FAILURE);
- }
- if (sigaction(SIGINT, &sigact, NULL) != 0)
- {
- fprintf(stderr, "Can't handle SIGINT. Error: %s\n", strerror(errno));
- exit(EXIT_FAILURE);
- }
- if (sigaction(SIGTERM, &sigact, NULL) != 0)
- {
- fprintf(stderr, "Can't handle SIGTERM. Error: %s\n", strerror(errno));
- exit(EXIT_FAILURE);
- }
- if (sigaction(SIGABRT, &sigact, NULL) != 0)
- {
- fprintf(stderr, "Can't handle SIGABRT. Error: %s\n", strerror(errno));
- exit(EXIT_FAILURE);
- }
- }
- static void openplatform_get_param_fun(void)
- {
- int msgid;
- msgbuf_ext msg;
-
- memset(&msg, 0, sizeof(msg));
-
- msgid = create_msgq(OPEN_APP_NAME, FALSE);
- if(msgid < 0)
- {
- fprintf(stderr, "%s create_msgq failed!!!\n", __FUNCTION__);
- exit(EXIT_FAILURE);
- }
-
- while(1)
- {
- memset(&msg, 0, sizeof(msg));
- msg.mtype = OPEN_PLATFORM_MSG_PRI_GET;
- if(recv_msg(msgid, &msg, sizeof(msg.buffer), OPEN_PLATFORM_MSG_PRI_GET, 0, OPEN_APP_NAME) == ERROR)
- {
- /* In general, here will be blocked, so usually will not enter here */
- fprintf(stderr, "%s recv_msg failed [%s]!!!\n", __FUNCTION__, strerror(errno));
- usleep(20000);
- exit(EXIT_FAILURE);
- }
- if(strlen(msg.buffer) == 0)
- {
- usleep(20000);
- continue;
- }
-
- fprintf(stderr, "%s recv [%s]\n", __FUNCTION__, msg.buffer);
- if(strcmp(msg.buffer, "GetConfig") == 0)
- {
- /* Do something */
- memset(&msg, 0, sizeof(msg));
- msg.mtype = OPEN_PLATFORM_MSG_PRI_GET_RESP;
- sprintf(msg.buffer, "%s", "I have get \"GetConfig\" request!");
- if(send_msg(msgid, &msg, strlen(msg.buffer), 0, OPEN_APP_NAME) == ERROR)
- {
- fprintf(stderr, "%s send_msg failed!!!\n", __FUNCTION__);
- continue;
- }
- }
- else
- {
- fprintf(stderr, "%s Recv msg is err!!!\n", __FUNCTION__);
- }
- }
- }
- static void openplatform_set_param_fun(void)
- {
- int msgid;
- msgbuf_ext msg;
-
- memset(&msg, 0, sizeof(msg));
-
- msgid = create_msgq(OPEN_APP_NAME, FALSE);
- if(msgid < 0)
- {
- fprintf(stderr, "%s create_msgq failed!!!\n", __FUNCTION__);
- exit(EXIT_FAILURE);
- }
-
- while(1)
- {
- memset(&msg, 0, sizeof(msg));
- msg.mtype = OPEN_PLATFORM_MSG_PRI_SET;
- if(recv_msg(msgid, &msg, sizeof(msg.buffer), OPEN_PLATFORM_MSG_PRI_SET, 0, OPEN_APP_NAME) == ERROR)
- {
- /* In general, here will be blocked, so usually will not enter here */
- fprintf(stderr, "%s recv_msg failed [%s]!!!\n", __FUNCTION__, strerror(errno));
- usleep(20000);
- exit(EXIT_FAILURE);
- }
- if(strlen(msg.buffer) == 0)
- {
- usleep(20000);
- continue;
- }
-
- fprintf(stderr, "%s recv [%s]\n", __FUNCTION__, msg.buffer);
- if(strncmp(msg.buffer, "SetConfig", strlen("SetConfig")) == 0)
- {
- /* Do something */
- memset(&msg, 0, sizeof(msg));
- msg.mtype = OPEN_PLATFORM_MSG_PRI_SET_RESP;
- sprintf(msg.buffer, "%s", "OK");
- if(send_msg(msgid, &msg, strlen(msg.buffer), 0, OPEN_APP_NAME) == ERROR)
- {
- fprintf(stderr, "send_msg failed!!!\n");
- continue;
- }
- }
- else
- {
- fprintf(stderr, "%s Recv msg is err!!!\n", __FUNCTION__);
- }
- }
- }
- static void openplatform_license_upload_fun(void)
- {
- int msgid;
- msgbuf_ext msg;
-
- memset(&msg, 0, sizeof(msg));
-
- msgid = create_msgq(OPEN_APP_NAME, FALSE);
- if(msgid < 0)
- {
- fprintf(stderr, "%s create_msgq failed!!!\n", __FUNCTION__);
- exit(EXIT_FAILURE);
- }
-
- while(1)
- {
- memset(&msg, 0, sizeof(msg));
- msg.mtype = OPEN_PLATFORM_MSG_PRI_UPLOAD_LICENSE;
- if(recv_msg(msgid, &msg, sizeof(msg.buffer), OPEN_PLATFORM_MSG_PRI_UPLOAD_LICENSE, 0, OPEN_APP_NAME) == ERROR)
- {
- /* In general, here will be blocked, so usually will not enter here */
- fprintf(stderr, "%s recv_msg failed [%s]!!!\n", __FUNCTION__, strerror(errno));
- usleep(20000);
- exit(EXIT_FAILURE);
- }
- if(strlen(msg.buffer) == 0)
- {
- usleep(20000);
- continue;
- }
-
- fprintf(stderr, "%s recv [%s]\n", __FUNCTION__, msg.buffer);
- if(strncmp(msg.buffer, "uploadLicense", strlen("uploadLicense")) == 0)
- {
- /* Do something */
- memset(&msg, 0, sizeof(msg));
- msg.mtype = OPEN_PLATFORM_MSG_PRI_UPLOAD_LICENSE_RESP;
- sprintf(msg.buffer, "%s", "OK");
- if(send_msg(msgid, &msg, strlen(msg.buffer), 0, OPEN_APP_NAME) == ERROR)
- {
- fprintf(stderr, "send_msg failed!!!\n");
- continue;
- }
- }
- else
- {
- fprintf(stderr, "%s Recv msg is err!!!\n", __FUNCTION__);
- }
- }
- }
- int main()
- {
- pthread_t set_pthread_id = -1;
- pthread_t get_pthread_id = -1;
- pthread_t license_pthread_id = -1;
- int err = -1;
- pthread_attr_t attri;
-
- memset(&attri, 0, sizeof(attri));
-
- signal_handler_init();
-
- /* Initialization thread attribute */
- pthread_attr_init(&attri);
-
- /* Set to separate threads, thread end of the release of their own resources, other properties with default values */
- err = pthread_attr_setdetachstate(&attri, PTHREAD_CREATE_DETACHED);
- if(err != 0)
- {
- pthread_attr_destroy(&attri);
- exit(EXIT_FAILURE);
- }
- err = pthread_create(&get_pthread_id, &attri, (void*)openplatform_get_param_fun, NULL);
- if(err != 0)
- {
- fprintf(stderr, "<openplatform_get_param_fun> pthread creat failed [%s]!\n", strerror(errno));
- exit(EXIT_FAILURE);
- }
- err = pthread_create(&set_pthread_id, &attri, (void*)openplatform_set_param_fun, NULL);
- if(err != 0)
- {
- fprintf(stderr, "<openplatform_set_param_fun> pthread creat failed [%s]!\n", strerror(errno));
- exit(EXIT_FAILURE);
- }
-
- err = pthread_create(&license_pthread_id, &attri, (void*)openplatform_license_upload_fun, NULL);
- if(err != 0)
- {
- fprintf(stderr, "<openplatform_license_upload_fun> pthread creat failed [%s]!\n", strerror(errno));
- exit(EXIT_FAILURE);
- }
- while(!terminated)
- {
- sleep(1);
- //fprintf(stderr, "open platform demo app!!\n");
- }
- return;
- }
复制代码 |
|