mirror of
https://github.com/Hizenberg469/C1-Linux_SYS_Prog-AS-.git
synced 2026-04-20 02:42:23 +03:00
Linux_sys_prog
This commit is contained in:
14
gluethread/glthreads/Makefile
Normal file
14
gluethread/glthreads/Makefile
Normal file
@@ -0,0 +1,14 @@
|
||||
LIBNAME=libglthread.a
|
||||
TARGET:${LIBNAME} exe
|
||||
${LIBNAME}:glthread.o
|
||||
ar rs ${LIBNAME} glthread.o
|
||||
exe:main.o ${LIBNAME}
|
||||
gcc -g main.o -o exe -L . -lglthread
|
||||
glthread.o:glthread.c
|
||||
gcc -g -c -I . glthread.c -o glthread.o
|
||||
main.o:main.c
|
||||
gcc -g -c -I . main.c -o main.o
|
||||
clean:
|
||||
rm *.o
|
||||
rm exe
|
||||
rm ${LIBNAME}
|
||||
81
gluethread/glthreads/glthread.c
Normal file
81
gluethread/glthreads/glthread.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include "glthread.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/*Private function to add a new_node right after curr_node*/
|
||||
static void
|
||||
_glthread_add_next(glthread_node_t *curr_node,
|
||||
glthread_node_t *new_node){
|
||||
|
||||
if(!curr_node->right){
|
||||
curr_node->right = new_node;
|
||||
new_node->left = curr_node;
|
||||
return;
|
||||
}
|
||||
|
||||
glthread_node_t *temp = curr_node->right;
|
||||
curr_node->right = new_node;
|
||||
new_node->left = curr_node;
|
||||
new_node->right = temp;
|
||||
temp->left = new_node;
|
||||
}
|
||||
|
||||
/*Fn to insert a new GLnode into a glthread at the first position
|
||||
* in a glthread i.e. new node becomes a head
|
||||
* */
|
||||
void
|
||||
glthread_add (glthread_t *lst, glthread_node_t *glnode){
|
||||
|
||||
glnode->left = NULL;
|
||||
glnode->right = NULL;
|
||||
if(!lst->head){
|
||||
lst->head = glnode;
|
||||
return;
|
||||
}
|
||||
glthread_node_t *head = lst->head;
|
||||
_glthread_add_next(glnode, head);
|
||||
lst->head = glnode;
|
||||
}
|
||||
|
||||
static void
|
||||
_remove_glthread(glthread_node_t *glnode){
|
||||
|
||||
if(!glnode->left){
|
||||
if(glnode->right){
|
||||
glnode->right->left = NULL;
|
||||
glnode->right = 0;
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(!glnode->right){
|
||||
glnode->left->right = NULL;
|
||||
glnode->left = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
glnode->left->right = glnode->right;
|
||||
glnode->right->left = glnode->left;
|
||||
glnode->left = 0;
|
||||
glnode->right = 0;
|
||||
}
|
||||
|
||||
/*API to remove a glnode from glthread*/
|
||||
void
|
||||
glthread_remove(glthread_t *lst, glthread_node_t *glnode){
|
||||
|
||||
glthread_node_t *head = lst->head;
|
||||
/*If the node being removed is the head node itself, then update the
|
||||
* head*/
|
||||
if(head == glnode){
|
||||
lst->head = head->right;
|
||||
}
|
||||
_remove_glthread(glnode);
|
||||
}
|
||||
|
||||
void
|
||||
init_glthread(glthread_t *glthread, unsigned int offset){
|
||||
|
||||
glthread->head = NULL;
|
||||
glthread->offset = offset;
|
||||
}
|
||||
42
gluethread/glthreads/glthread.h
Normal file
42
gluethread/glthreads/glthread.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef __GLTHREADS__
|
||||
#define __GLTHREADS__
|
||||
|
||||
typedef struct glthread_node_ {
|
||||
|
||||
struct glthread_node_ *left;
|
||||
struct glthread_node_ *right;
|
||||
} glthread_node_t;
|
||||
|
||||
typedef struct glthread_ {
|
||||
|
||||
glthread_node_t *head;
|
||||
unsigned int offset;
|
||||
} glthread_t;
|
||||
|
||||
void
|
||||
glthread_add (glthread_t *lst, glthread_node_t *glnode);
|
||||
|
||||
void
|
||||
glthread_remove(glthread_t *lst, glthread_node_t *glnode);
|
||||
|
||||
/*Iterative macro to Iterate Over GLTHhreads*/
|
||||
#define ITERATE_GL_THREADS_BEGIN(lstptr, struct_type, ptr) \
|
||||
{ \
|
||||
glthread_node_t *_glnode = NULL, *_next = NULL; \
|
||||
for(_glnode = lstptr->head; _glnode; _glnode = _next){ \
|
||||
_next = _glnode->right; \
|
||||
ptr = (struct_type *)((char *)_glnode - lstptr->offset);
|
||||
|
||||
#define ITERATE_GL_THREADS_ENDS }}
|
||||
|
||||
#define glthread_node_init(glnode) \
|
||||
glnode->left = NULL; \
|
||||
glnode->right = NULL;
|
||||
|
||||
void
|
||||
init_glthread(glthread_t *glthread, unsigned int offset);
|
||||
|
||||
#define offsetof(struct_name, field_name) \
|
||||
((unsigned int)&((struct_name *)0)->field_name)
|
||||
|
||||
#endif /* __GLTHREADS__ */
|
||||
87
gluethread/glthreads/main.c
Normal file
87
gluethread/glthreads/main.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "glthread.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <memory.h>
|
||||
|
||||
typedef struct emp_ {
|
||||
|
||||
char name[30];
|
||||
unsigned int salary;
|
||||
char designation[30];
|
||||
unsigned int emp_id;
|
||||
glthread_node_t glnode;
|
||||
} emp_t;
|
||||
|
||||
void
|
||||
print_emp_details(emp_t *emp){
|
||||
|
||||
printf("Employee name = %s\n", emp->name);
|
||||
printf("salary = %u\n", emp->salary);
|
||||
printf("designation = %s\n", emp->designation);
|
||||
printf("emp_id = %u\n", emp->emp_id);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char **argv){
|
||||
|
||||
emp_t *emp1 = calloc(1, sizeof(emp_t));
|
||||
strncpy(emp1->name, "Neha", strlen("Neha"));
|
||||
emp1->salary = 50000;
|
||||
strncpy(emp1->designation, "HR", strlen("HR"));
|
||||
emp1->emp_id = 21;
|
||||
glthread_node_init((&emp1->glnode));
|
||||
|
||||
emp_t *emp2 = calloc(1, sizeof(emp_t));
|
||||
strncpy(emp2->name, "Abhishek", strlen("Abhishek"));
|
||||
emp1->salary = 150000;
|
||||
strncpy(emp2->designation, "SE3", strlen("SE3"));
|
||||
emp2->emp_id = 32;
|
||||
glthread_node_init((&emp2->glnode));
|
||||
|
||||
emp_t *emp3 = calloc(1, sizeof(emp_t));
|
||||
strncpy(emp3->name, "Arun", strlen("Arun"));
|
||||
emp3->salary = 60000;
|
||||
strncpy(emp3->designation, "SE4", strlen("SE4"));
|
||||
emp3->emp_id = 27;
|
||||
glthread_node_init((&emp3->glnode));
|
||||
|
||||
|
||||
/*Now Create a glthread*/
|
||||
glthread_t *emp_list = calloc(1, sizeof(glthread_t));
|
||||
init_glthread(emp_list, offsetof(emp_t, glnode));
|
||||
|
||||
/*Now insert the records in glthread*/
|
||||
glthread_add(emp_list, &emp1->glnode);
|
||||
glthread_add(emp_list, &emp2->glnode);
|
||||
glthread_add(emp_list, &emp3->glnode);
|
||||
|
||||
|
||||
/*Walk over glthread*/
|
||||
emp_t *ptr = NULL;
|
||||
ITERATE_GL_THREADS_BEGIN(emp_list, emp_t, ptr){
|
||||
|
||||
print_emp_details(ptr);
|
||||
} ITERATE_GL_THREADS_ENDS;
|
||||
|
||||
/*Let us remove one record at random*/
|
||||
|
||||
glthread_remove(emp_list, &emp2->glnode);
|
||||
printf("\nprinting again . . . \n");
|
||||
|
||||
ITERATE_GL_THREADS_BEGIN(emp_list, emp_t, ptr){
|
||||
|
||||
print_emp_details(ptr);
|
||||
} ITERATE_GL_THREADS_ENDS;
|
||||
|
||||
|
||||
/*Free all Dynamicall allocations*/
|
||||
ITERATE_GL_THREADS_BEGIN(emp_list, emp_t, ptr){
|
||||
|
||||
glthread_remove(emp_list, &ptr->glnode);
|
||||
free(ptr);
|
||||
} ITERATE_GL_THREADS_ENDS;
|
||||
free(emp_list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
87
gluethread/glthreads/main2.c
Normal file
87
gluethread/glthreads/main2.c
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "glthread.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <memory.h>
|
||||
|
||||
typedef struct emp_ {
|
||||
|
||||
char name[30];
|
||||
unsigned int salary;
|
||||
char designation[30];
|
||||
unsigned int emp_id;
|
||||
glthread_node_t glnode;
|
||||
} emp_t;
|
||||
|
||||
void
|
||||
print_emp_details(emp_t *emp){
|
||||
|
||||
printf("Employee name = %s\n", emp->name);
|
||||
printf("salary = %u\n", emp->salary);
|
||||
printf("designation = %s\n", emp->designation);
|
||||
printf("emp_id = %u\n", emp->emp_id);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main(int argc, char **argv){
|
||||
|
||||
emp_t *emp1 = calloc(1, sizeof(emp_t));
|
||||
strncpy(emp1->name, "Neha", strlen("Neha"));
|
||||
emp1->salary = 50000;
|
||||
strncpy(emp1->designation, "HR", strlen("HR"));
|
||||
emp1->emp_id = 21;
|
||||
glthread_node_init((&emp1->glnode));
|
||||
|
||||
emp_t *emp2 = calloc(1, sizeof(emp_t));
|
||||
strncpy(emp2->name, "Abhishek", strlen("Abhishek"));
|
||||
emp1->salary = 150000;
|
||||
strncpy(emp2->designation, "SE3", strlen("SE3"));
|
||||
emp2->emp_id = 32;
|
||||
glthread_node_init((&emp2->glnode));
|
||||
|
||||
emp_t *emp3 = calloc(1, sizeof(emp_t));
|
||||
strncpy(emp3->name, "Arun", strlen("Arun"));
|
||||
emp3->salary = 60000;
|
||||
strncpy(emp3->designation, "SE4", strlen("SE4"));
|
||||
emp3->emp_id = 27;
|
||||
glthread_node_init((&emp3->glnode));
|
||||
|
||||
|
||||
/*Now Create a glthread*/
|
||||
glthread_t *emp_list = calloc(1, sizeof(glthread_t));
|
||||
init_glthread(emp_list, offsetof(emp_t, glnode));
|
||||
|
||||
/*Now insert the records in glthread*/
|
||||
glthread_add(emp_list, &emp1->glnode);
|
||||
glthread_add(emp_list, &emp2->glnode);
|
||||
glthread_add(emp_list, &emp3->glnode);
|
||||
|
||||
|
||||
/*Walk over glthread*/
|
||||
emp_t *ptr = NULL;
|
||||
ITERATE_GL_THREADS_BEGIN(emp_list, emp_t, ptr){
|
||||
|
||||
print_emp_details(ptr);
|
||||
} ITERATE_GL_THREADS_ENDS;
|
||||
|
||||
/*Let us remove one record at random*/
|
||||
|
||||
glthread_remove(emp_list, &emp2->glnode);
|
||||
printf("\nprinting again . . . \n");
|
||||
|
||||
ITERATE_GL_THREADS_BEGIN(emp_list, emp_t, ptr){
|
||||
|
||||
print_emp_details(ptr);
|
||||
} ITERATE_GL_THREADS_ENDS;
|
||||
|
||||
|
||||
/*Free all Dynamicall allocations*/
|
||||
ITERATE_GL_THREADS_BEGIN(emp_list, emp_t, ptr){
|
||||
|
||||
glthread_remove(emp_list, &ptr->glnode);
|
||||
free(ptr);
|
||||
} ITERATE_GL_THREADS_ENDS;
|
||||
free(emp_list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user