当前位置:网站首页>Assignment 1 - Hello World ! - Simple thread Creation
Assignment 1 - Hello World ! - Simple thread Creation
2022-07-27 20:15:00 【Sour bean girl】
Assignment 1 - Hello World ! - Simple thread Creation
Requirement
- Print the output of uname -a in the first line of the syslog file.
- Each syslog statement should have [COURSE:X][ASSIGNMENT:Y] where X corresponds to Course Number i.e. 1 in this case and Y corresponds to Assignment No.
Example - Aug 30 22:10:54 raspberrypi pthread:
[COURSE:1][ASSIGNMENT:1] YOUR DATA HERE
This is what your output should look like. Also, please note that the uname output is generated from the program and not copied from the terminal and pasted into the program as a string.
All syslog prints in this assignment and upcoming assignments shall follow this format. (The time, name of the machine, and the program name are generated by syslog, you do not need to add them in manually).
3. This assignment expects two log statements as below from the main and the thread in execution
Hello World from Main!
Hello World from Thread!
Solution
1. how to log uname -a info into syslog?
a. use system function
- using man system to get how to use this interface. please note that we need to #include <stdlib.h>
system("logger [COURSE:1][ASSIGNMENT:1]:`uname -a`");`
b. print uname struct elements (this makes things a little bit complicated)
- uname struct
struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined network" */
char release[]; /* Operating system release (e.g., "2.6.28") */
char version[]; /* Operating system version */
char machine[]; /* Hardware identifier */
#ifdef _GNU_SOURCE
char domainname[]; /* NIS or YP domain name */
#endif
};
reference: https://www.man7.org/linux/man-pages/man2/uname.2.html
- corresponding code
struct utsname *buf = malloc(sizeof(struct utsname));
uname(buf);
syslog(LOG_INFO, "%s %s %s %s %s GNU/Linux\n", buf->sysname, buf->nodename, buf->release, buf->version, buf->machine);
it has disadvantages since there is no operation system info in the struct.
2. how to use syslog
- openlog() and closelog()
3. the whole code
#include <pthread.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <stdio.h>
#include <sched.h>
#include <syslog.h>
#define NUM_THREADS 1
#define COURSE_INFO "[COURSE:1][ASSIGNMENT:1]"
typedef struct
{
int threadIdx;
} threadParams_t;
// POSIX thread declarations and scheduling attributes
//
pthread_t threads[NUM_THREADS];
threadParams_t threadParams[NUM_THREADS];
void *counterThread(void *threadp)
{
openlog(COURSE_INFO, LOG_CONS, LOG_USER);
syslog(LOG_INFO, "Hello World from Thread!");
closelog();
return 0;
}
int main (int argc, char *argv[])
{
int i;
struct utsname *buf = malloc(sizeof(struct utsname));
uname(buf);
openlog(COURSE_INFO, LOG_CONS, LOG_USER);
syslog(LOG_INFO, "%s %s %s %s %s GNU/Linux\n", buf->sysname, buf->nodename, buf->release, buf->version, buf->machine);
// I know we could use system call cmd to print uname, but here I keep my original code which print the uname struct elements one by one
#if (0)
system("logger [COURSE:1][ASSIGNMENT:1]: `uname -a`");
#endif
syslog(LOG_INFO, "Hello World from Main!");
for(i=0; i < NUM_THREADS; i++)
{
threadParams[i].threadIdx=i;
pthread_create(&threads[i], // pointer to thread descriptor
(void *)0, // use default attributes
counterThread, // thread function entry point
(void *)&(threadParams[i]) // parameters to pass in
);
}
for(i=0;i<NUM_THREADS;i++)
pthread_join(threads[i], NULL);
closelog();
}
边栏推荐
- ViewUI 中 DatePicker 日期选择器在 IE11 浏览器中兼容解决方案
- 华为150人团队驰援,武汉“小汤山”5G基站火速开通!
- 22 year PMP test [Quanzhen agile test]
- 解决 ViewUI 表格无数据时展示滚动条的问题
- Western digital mobile hard disk can't be read (the idiom of peace of mind)
- PyQt5快速开发与实战 4.5 按钮类控件 and 4.6 QComboBox(下拉列表框)
- Common errors reported by pytorch
- uva1421
- antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
- LED high precision scale scheme specification
猜你喜欢

PyQt5快速开发与实战 4.3 QLabel and 4.4 文本框类控件

一看就懂的ESLint

办公自动化解决方案——DocuWare Cloud 将应用程序和流程迁移到云端的完整的解决方案

Underlying principle of mvcc

Qtexttospeech class of QT realizes voice broadcast function

Technology sharing | how to do Assertion Verification in interface automated testing?

C# 后台GC 的前因后果

总线Bus是什么意思

#yy关于鱼的英文学习

ms721负载测试
随机推荐
[openbmc series] 4. Start the process and use qume to simulate ast2600 EVB
十年测试老鸟聊聊移动端兼容性测试
传英特尔将停掉台积电16nm代工的Nervana芯片
[redis] several deployment methods of redis
邬贺铨:因地制宜 数字化技术赋能“双碳”实践
SQLite SQL writing method of creating table joint primary key
Zepto入门详解
C # network application programming, experiment 1: WPF exercise
Konka sold out its first 100000 storage master chips, with an estimated sales volume of 100million in 2020
一看就懂的ESLint
[C # network application programming] Experiment 3: process management exercise
Technology sharing | how to do Assertion Verification in interface automated testing?
Kubectl's access to pod logs -- the way to build a dream
[Redis] Redis穿透、雪崩和击穿
Use cpolar to build a business website (5)
西数移动硬盘无法读取(高枕无忧的成语)
Product Manager: check where there is an error prompt of "system exception" on the offline
2019年全球半导体市场收入4183亿美元,同比下滑11.9%
信道容量、信道带宽基本概念的理解
uva1421