SP Programming Model Program
SP Programming Model Program
Define a data structure of type 'book' containing the info 'book name', 'author', 'pages' &
'price'. Write a program to read & display these data using pointer.
#include <stdio.h>
#include <stdlib.h>
struct Book {
char name[100];
char author[100];
int pages;
float price;
};
int main() {
if (ptr == NULL) {
return 1;
scanf("%s", ptr->name);
scanf("%s", ptr->author);
scanf("%d", &ptr->pages);
scanf("%f", &ptr->price);
printf("\nBook Information:\n");
free(ptr);
return 0;
2. Write a simple function in 'C' that returns a structure. Demonstrate with an example.
#include <stdio.h>
struct Person {
char name[100];
int age;
};
scanf("%s", newPerson.name);
scanf("%d", &newPerson.age);
return newPerson;
int main() {
printf("\nPerson Information:\n");
return 0;
3. Write a program to multiply two - 8bit numbers using 8051 or any other microcontroller.
#include <stdio.h>
return a * b;
}
int main() {
return 0;
4. Write the steps taken after receiving an interrupt in 8051 architecture. Give an example
PROGRAM EXPLANATION
ORG 0x00 ;
START:
SETB ET0 ;
SETB EA ;
SETB TR0 ;
MAIN_LOOP:
NOP ;
TIMER0_ISR:
RETI ;
5. Create two threads 'T1' & 'T2'. The thread T1 shall read the data from stdin continuously, the
other thread 'T2' will write the data by 'T1' to a file "output.txt". Develop a program in C to
demonstrate this.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct SharedData {
FILE *file;
pthread_mutex_t mutex;
int stopFlag;
};
char buffer[100];
while (!sharedData->stopFlag) {
pthread_mutex_lock(&sharedData->mutex);
pthread_mutex_unlock(&sharedData->mutex);
if (strcmp(buffer, "exit\n") == 0) {
sharedData->stopFlag = 1;
return NULL;
while (!sharedData->stopFlag) {
usleep(100000);
return NULL;
int main() {
if (outputFile == NULL) {
return 1;
sharedData.stopFlag = 0;
pthread_mutex_init(&sharedData.mutex, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_mutex_destroy(&sharedData.mutex);
fclose(outputFile);
return 0;
6. Write a program to copy the contents of a text file "a.txt" to another file "b.txt" & count the
occurrence of 'the' in the file "a.txt".
#include <stdio.h>
#include <stdlib.h>
int main() {
char ch;
int count = 0;
exit(1);
fputc(ch, fileB);
if (ch == 't') {
ch = fgetc(fileA);
if (ch == 'h') {
ch = fgetc(fileA);
if (ch == 'e') {
count++;
ungetc(ch, fileA);
} else {
ungetc(ch, fileA);
fclose(fileA);
fclose(fileB);
return 0;
7. Write a signal handler in C which prints your name when you press "ctrl+c".
#include <stdio.h>
#include <signal.h>
int main() {
signal(SIGINT, signalHandler);
while (1) {
return 0;
}
8. Write a program to demonstrate two processes communicating with each other using shared
memory.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
struct SharedData {
int number;
char message[100];
};
int main() {
int shmid;
if (shmid == -1) {
perror("shmget");
exit(1);
if ((intptr_t)sharedData == -1) {
perror("shmat");
exit(1);
if (pid == -1) {
perror("fork");
exit(1);
} else if (pid == 0) {
printf("Child Process:\n");
shmdt(sharedData);
} else {
printf("Parent Process:\n");
sharedData->number = 42;
wait(NULL);
shmdt(sharedData);
return 0;
9. Write a program to add a data byte located at offset 0x0500 in 0x2000 usegment to another
data byte at 0x0600 in the same segment & store the result at 0x700 in the same segment
#include <stdio.h>
int main() {
char userSegment[0x2000];
userSegment[0x0500] = 0x0A;
userSegment[0x0600] = 0x05;
return 0;
10. Write a program in Assembly for a 16-bit increment without affecting the contents of the
accumulator
section .data
value dw 1234h
section .text
global _start
_start:
inc ax
; AX now contains the incremented value, and the original value is still in [value]
mov eax, 1
int 0x80
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main() {
signal(SIGALRM, alarmHandler);
alarm(2);
while (1) {
sleep(1);
return 0;