【发布时间】:2021-05-06 01:22:02
【问题描述】:
所有堆栈溢出窥视你好!
在我的程序中有此代码会因错误而退出...但成功了吗? 不知道为什么?
输出:
- 日期时间 - RLIMIT_RTTIME:软=-1,硬=-1
- dateTime - RLIMIT_RTPRIO: soft=-1, hard=-1
- dateTime - RLIMIT_CPU: soft=-1, hard=-1
- dateTime - main() - pthread_attr_setschedParam()
- 成功
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
char strNowTime[26];
//-----------------------------------
// signal Handler stuff.
//-----------------------------------
static
struct sigaction mySigActTerm;
volatile
int myTerminate = 0;
void terminateHandler(int signum, siginfo_t *info, void *ptr)
{
// set a flag here and get out.
myTerminate = 1;
}
void getNowTime(char* str)
{
time_t rawtime;
time(&rawtime);
ctime_r(&rawtime, str);
// clobber the unwanted newline.
str[24] = '\0';
}
void myResLimit()
{
struct
rlimit procLimit;
getrlimit(RLIMIT_RTTIME, &procLimit);
getNowTime(strNowTime);
fprintf(stderr, "%s - RLIMIT_RTTIME: soft=%lld, hard=%lld\n", strNowTime, (long long) procLimit.rlim_cur, (long long)procLimit.rlim_max);
getrlimit(RLIMIT_RTPRIO, &procLimit);
getNowTime(strNowTime);
fprintf(stderr, "%s - RLIMIT_RTPRIO: soft=%lld, hard=%lld\n", strNowTime, (long long) procLimit.rlim_cur, (long long) procLimit.rlim_max);
getrlimit(RLIMIT_CPU, &procLimit);
getNowTime(strNowTime);
fprintf(stderr, "%s - RLIMIT_CPU: soft=%lld, hard=%lld\n", strNowTime, (long long) procLimit.rlim_cur, (long long) procLimit.rlim_max);
}
void* serialThread(void* arg)
{
while (1) {
}
}
//-----------------------------------
// the one and only MAIN.
//-----------------------------------
int main()
{
//-----------------------------------------------
// locals.
int rtn;
int myErr;
pthread_t serialThdID;
pthread_attr_t* serialAttr;
struct
sched_param serialParam;
//-----------------------------------------------
// Log OS resource limits.
myResLimit();
//-----------------------------------------------
// initialize the signals struct.
// ... and setup signals.
memset(&mySigActTerm, 0, sizeof(mySigActTerm));
mySigActTerm.sa_sigaction = terminateHandler;
mySigActTerm.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &mySigActTerm, NULL);
//-----------------------------------------------
// setup the pthread attributes struct.
if ((serialAttr = malloc(sizeof(pthread_attr_t))) == NULL) {
myErr = errno;
getNowTime(strNowTime);
fprintf(stderr, "%s - main() - pthread_attr_t malloc()\n%s\n", strNowTime, strerror(myErr));
exit(EXIT_FAILURE);
}
memset(serialAttr, 0, sizeof(pthread_attr_t));
//-----------------------------------------------
// set initial default pthread attr values.
if (pthread_attr_init(serialAttr) != 0) {
myErr = errno;
getNowTime(strNowTime);
fprintf(stderr, "%s - main() - pthread_attr_init()\n%s\n", strNowTime, strerror(myErr));
exit(EXIT_FAILURE);
}
//-----------------------------------------------
// set for best near real time policy.
if (pthread_attr_setschedpolicy(serialAttr, SCHED_FIFO) !=0) {
myErr = errno;
getNowTime(strNowTime);
fprintf(stderr, "%s - main() - pthread_attr_setschedpolicy()\n%s\n", strNowTime, strerror(myErr));
exit(EXIT_FAILURE);
}
//-----------------------------------------------
// set to explicit inherit or attr obj will be ignored.
if (pthread_attr_setinheritsched(serialAttr, PTHREAD_EXPLICIT_SCHED) !=0) {
myErr = errno;
getNowTime(strNowTime);
fprintf(stderr, "%s - main() - pthread_attr_setinheritsched()\n%s\n", strNowTime, strerror(myErr));
exit(EXIT_FAILURE);
}
//-----------------------------------------------
// set to un-limited thread priority.
serialParam.sched_priority = 0;
if (pthread_attr_setschedparam(serialAttr, &serialParam) !=0) {
myErr = errno;
getNowTime(strNowTime);
fprintf(stderr, "%s - main() - pthread_attr_setschedparam()\n%s\n", strNowTime, strerror(myErr));
exit(EXIT_FAILURE);
}
//-----------------------------------------------
// start the new thread.
rtn = pthread_create(&serialThdID, serialAttr, serialThread, NULL);
myErr = errno;
if(rtn == 0) {
getNowTime(strNowTime);
fprintf(stderr, "%s - starting serial thread.\n", strNowTime);
}
else {
getNowTime(strNowTime);
fprintf(stderr, "%s - main() - pthread_create() returned %d\n%s\n", strNowTime, rtn, strerror(myErr));
exit(EXIT_FAILURE);
}
//-----------------------------------------------
// no need to keep this junk if pthread_create() succeeded.
if (pthread_attr_destroy(serialAttr) != 0) {
myErr = errno;
getNowTime(strNowTime);
fprintf(stderr, "%s - main() - pthread_attr_destroy()\n%s\n", strNowTime, strerror(myErr));
}
// research proves this is needed if we malloc'ed!
free(serialAttr);
while (myTerminate == 0) {
}
}
【问题讨论】:
-
与你的问题无关,但是没有理由使用
malloc来获取属性对象。只需声明一个pthread_attr_t类型的对象而不是指向一个对象的指针,然后使用它的地址。