Embedded Linux Unit3 Detailed Notes
Embedded Linux Unit3 Detailed Notes
System programming refers to writing code that directly interacts with the operating system using
system calls. These include process control, file manipulation, device management, etc.
Common headers:
- <unistd.h>: Declares standard symbolic constants and types, includes read(), write(), fork(), etc.
- <fcntl.h>: File control options for open(), etc.
We can use system calls to perform file operations (open, read, write, close).
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("example.txt", O_CREAT | O_WRONLY, 0644);
write(fd, "File I/O in Linux\n", 18);
close(fd);
return 0;
}
3. IO Operation Programming
#include <fcntl.h>
#include <unistd.h>
int main() {
char buffer[100];
int fd = open("example.txt", O_RDONLY);
int n = read(fd, buffer, 100);
write(1, buffer, n);
close(fd);
return 0;
}
Here, we:
- Read up to 100 bytes from the file
- Write the content to stdout (fd = 1)
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
execlp("ls", "ls", NULL);
} else {
wait(NULL);
printf("Child process finished\n");
}
return 0;
}
exec() can be used without fork(), but it will replace the current process image, so no code after
exec() runs.
exit() vs _exit():
- exit(): flushes stdio buffers, runs cleanup handlers
- _exit(): immediately terminates, no flushing
Flushing means writing any buffered output (like printf) to output files or screen.
#include <stdio.h>
#include <pthread.h>
int main() {
pthread_t tid;
pthread_create(&tid, NULL, print_message, NULL);
pthread_join(tid, NULL);
return 0;
}
Race condition: multiple threads access shared data at the same time.
#include <stdio.h>
#include <pthread.h>
int counter = 0;
pthread_mutex_t lock;
void* increment(void* arg) {
for (int i = 0; i < 100000; i++) {
pthread_mutex_lock(&lock);
counter++;
pthread_mutex_unlock(&lock);
}
return NULL;
}
int main() {
pthread_t t1, t2;
pthread_mutex_init(&lock, NULL);
pthread_create(&t1, NULL, increment, NULL);
pthread_create(&t2, NULL, increment, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&lock);
printf("Final counter: %d\n", counter);
return 0;
}