TLB miss on store. Segmentation fault in userland

I have just finished fork implementation and I run into a segmentation fault in the user land caused by the parent process when trying to printf something.

my fork test code that runs in usermode:

int main(void)
printf(“Forking my self\n”);

pid_t pid = fork();
if(pid != 0)
            write(1, "hello\n", 6);
	printf("I am the parent\n");


The segmentation fault occurs when running the printf(“I am the parent\n”) and it’s weird because when I try printing “I am the parent” with write() syscall it does not produce a segmentation fault and it prints hello. and when I try printing something with printf() from the forked child i.e if(pid == 0) it also prints and exits successfully. Any idea why might printf fails in the parent process with a segmentation fault when every other instruction in the parent process would succeed ?

This could have nothing to do with printf in particular. There could be certain sequence of instructions that are executed when printf is used, which might lead to a race condition. I would look at where exactly you are hitting the segmentation_fault. I suspect something in sys_write or the way file_table is copied from parent to child process in sys_fork might be going wrong.

Thanks a lot for your reply. After A lot of debugging and code reviewing I figured I was not correctly setting the user stack pointer and this was causing a segmentation fault in printf in particular aside from all other functions before it even gets to issue the write system call