linux C: 单链表,多线程和指针练习
2023-09-27 14:26:51 时间
1) 单链表程序
产生一个节点,向其节点存入结构体信息,并且通过指针将这些节点链接起来,组成单链表,然后从头到尾输出这个链表中节点的信息,最后释放这个链表中所有节点。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
typedef struct student{
char name[30];
unsigned int age;
float score;
struct student * next;
}student_t;
int main(int argc, char * argv[])
{
student_t * head = NULL;
student_t * new;
student_t * stuptr = NULL;
student_t * list;
char * names[] = {"LiLei", "HanMeimei", "Lucy", "Lily", "Jim"};
unsigned int ages[] = {10, 11, 8, 12, 7};
float scores[] = {4.0, 3.5, 2.7, 3.6, 2.1};
int i = 0;
while ( i < 5)
{
new = (student_t *)malloc(sizeof(student_t));
if (new == NULL)
{
fprintf(stderr, "Unable to allocate memory.\n");
while (head != NULL)
{
stuptr = head->next;
free(head);
head = stuptr;
}
exit(1);
}
else {
strcpy(new->name, names[i]);
new->age = ages[i];
new->score = scores[i];
new->next = head;
head = new;
i++;
}
}
list = head;
while (head)
{
printf("name: %s, age: %u, socre: %.2f\n",head->name, head->age, head->score);
head = head->next;
}
head = list;
while (head)
{
stuptr = head->next;
free(head);
head = stuptr;
}
return 0;
}
编译以上程序,执行结果:
[blctrl@bjAli C_Program]$ ./linkedlist
name: Jim, age: 7, socre: 2.10
name: Lily, age: 12, socre: 3.60
name: Lucy, age: 8, socre: 2.70
name: HanMeimei, age: 11, socre: 3.50
name: LiLei, age: 10, socre: 4.00
2) 多线程程序:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
pthread_create()函数在调用进程中启动一个新线程。这个新线程通过调用start_routine()启动执行。arg作为start_routine()的唯一参数被传递。
int pthread_join(pthread_t thread, void **retval);
pthread_join()函数等待由线程指定的这个线程终止。如果这个线程已经终结,则pthread_join()立即返回。由线程指定的这个线程必须时joinable。如果retval不是NULL,则pthread_join()复制这个目标线程的退出状态到由retval指向的位置(即:目标线程提供给pthread_exit的值)。如果目标线程被取消,则PTHREAD_CANCELED被放入到由retval指向的位置。
#include <stdio.h>
#include <pthread.h>
/*
* compile command:gcc -o pthread pthread.c -lpthread
* */
static void * myfunc(void * myvar);
int main(int argc, char * argv[])
{
pthread_t thread1, thread2;
char * msg1 = "First Thread";
char * msg2 = "Second Thread";
int ret1, ret2;
ret1 = pthread_create(&thread1, NULL, myfunc, (void *)msg1);
ret2 = pthread_create(&thread2, NULL, myfunc, (void *)msg2);
printf("Main function after pthread_create\n");
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("First thread ret1 = %d\n", ret1);
printf("Second thread ret1 = %d\n", ret2);
return 0;
}
void * myfunc(void * myvar)
{
char * msg;
msg = (char *)myvar;
int i;
for (i = 0 ; i < 3; i++)
{
printf("%s %d\n", msg, i);
sleep(1);
}
return NULL;
}
编译以上程序执行结果:
[blctrl@bjAli C_Program]$ ./pthread
Main function after pthread_create
First Thread 0
Second Thread 0
First Thread 1
Second Thread 1
First Thread 2
Second Thread 2
First thread ret1 = 0
Second thread ret1 = 0
3) 指针练习
#include <stdio.h>
int main(int argc, char * argv[])
{
int a[] = {1025,1026};
int * intptr = a;
char * charptr = (char *)intptr;
printf("a = %p, intptr = %p, intptr+1 = %p ,a[0] = %d, a[1] = %d, *intptr = %d, *(intpyt + 1) = %d\n",
a, intptr, intptr + 1, a[0], a[1], * intptr, *(intptr + 1));
printf("charptr = %p, charptr+1 = %p, *charptr=%d, *(charptr+1)=%d\n",
charptr, charptr+1, *(charptr), *(charptr+1));
return 0;
}
以上程序编译执行结果:
%p打印地址,当为int类型指针时,指针加1,一次移动4个字节,当为char类型指针时,指针加1,只移动一个字节。
[blctrl@main-machine debug]$ ./pointer
a = 0x7ffcaf49e788, intptr = 0x7ffcaf49e788, intptr+1 = 0x7ffcaf49e78c ,a[0] = 1025, a[1] = 1026, *intptr = 1025, *(intpyt + 1) = 1026
charptr = 0x7ffcaf49e788, charptr+1 = 0x7ffcaf49e789, *charptr=1, *(charptr+1)=4
4) 函数指针
定义一个函数指定,其接收的参数类型以及返回的值类型必须与所指向的函数一样,通过直接调用函数以及函数指针来调用函数,比较二者产生的结果:
#include <stdio.h>
int add_numbers(int a, int b)
{
return a + b;
}
int main(int argc, char * argv[])
{
int result;
int (* myfunc_ptr)(int, int);
result = add_numbers(3,7);
printf("result = %d\n", result);
myfunc_ptr = &add_numbers;
result = myfunc_ptr(3, 7);
printf("result = %d\n", result);
return 0;
}
编译以上代码,执行结果如下:
[blctrl@bjAli C_Program]$ ./func_pointer
result = 10
result = 10
相关文章
- Linux多线程实践(5) --Posix信号量与互斥量解决生产者消费者问题
- Linux多线程实践(3) --线程属性
- 修改Linux服务器时间的命令
- Linux--进程多线程(上)
- 学习Linux tar 命令:最简单也最困难
- Linux环境多线程编程基础设施
- Linux:【chmod (change mode) : 修改用户的权限】【chown (change owner) : 修改所属用户与组】
- Linux系统编程-线程(二)-线程同步05:信号量
- 如何在Amazon AWS上设置一台Linux服务器
- linux 下使用 sar -n 命令查看Kbps、bps的带宽速率
- linux 中多线程使用
- Linux下编译LibCURL
- Linux多线程实践(一)线程基本概念和理论
- Linux多线程编程详细解析----条件变量 pthread_cond_t
- Linux环境编写脚本安装配置JDK,Tomcat,含Tomcat自启动
- linux系统优化基础
- Linux下多线程下载工具myget
- 批量远程执行linux服务器程序--基于paramiko(多线程版)
- 【线程】linux之多线程同步互斥技术
- linux多线程示例
- Linux下多线程查看工具(pstree、ps、pstack)
- Linux 下wifi 驱动开发(三)—— SDIO接口WiFi驱动浅析(转)
- Linux下多线程查看工具(pstree、ps、pstack),linux命令之-pstree使用说明, linux 查看线程状态。 不指定
- Linux 多线程调试(内存占用、死循环、CPU占用率高……)
- 嵌入式linux/鸿蒙开发板(IMX6ULL)开发(十七)多线程编程
- linux(centos 6/7/8,kylin)下记录所有用户的操作以及ip、时间
- 启动期间的内存管理之build_zonelists初始化备用内存域列表zonelists--Linux内存管理(十三)
- Linux 多线程调试(内存占用、死循环、CPU占用率高……)